我创建了一个类别模型。我还创建了一个项目模型。项目模型属于类别模型,因此当您创建新项目时,您将收到一个类别下拉列表,以选择您想要的类别。
其中一个类别是"Root"我不希望这个显示在下拉列表中。我创建了belongsTo方法,像这样
<标题> project.php模型var $belongsTo = array(
'User' => array(
'className' => 'User',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Category' => array(
'className' => 'Category',
'conditions' => array('Category.id '=>'1'),
'fields' => '',
'order' => ''
),
);
对于我的控制器,我有脚手架打开。
这是我的分类模型
<标题> 分类模型class Category extends AppModel {
var $name = 'Category';
var $displayField = 'name';
var $actsAs = array('Tree');
var $validate = array(
'name' => array(
'alphanumeric' => array(
'rule' => array('alphanumeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'parent_id' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'url' => array(
'notempty' => array(
'rule' => array('notempty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
var $belongsTo = array(
'ParentCategory' => array(
'className' => 'Category',
'conditions' => '',
'foreignKey' => 'parent_id',
'fields' => '',
'order' => ''
),
);
}
标题>标题>
我想你的意思是从下拉菜单中删除Root
蛋糕产生的关联?在这种情况下,试着这样做:
$categories = $this->Category->find('list',
array('conditions' => array('Category.name !=' => 'Root')));
$this->set(compact('categories'));
用'conditions' => array('Categories !=' =>'1'),
代替。
验证用于保存数据,而不是查找数据。