我創建了一個類別模型。我還創建了一個項目模型。項目模型屬於類別模型,因此當您創建新項目時,您會收到一個類別下拉列表以選擇您想要的類別。無法過濾cakephp模型中的值
其中一個類別是「根」,我不希望它顯示在下拉列表中。我創建了屬於關聯方法,像這樣
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' => ''
),
);
}