我创建了一个类别模型。我还创建了一个项目模型。项目模型属于类别模型,因此当您创建一个新项目时,您会收到一个类别下拉列表来选择您想要的类别。
其中一个类别是“根”,我不希望它显示在下拉列表中。我像这样创建了我的 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' => ''
),
);
}