开发者

can not filter values within cakephp model

开发者 https://www.devze.com 2023-04-09 11:47 出处:网络
I created a categories model. I also created a project model. The project model belongs to the categories model so when you create a new project, you recieve a category drop down to pick which categor

I created a categories model. I also created a project model. The project model belongs to the categories model so when you create a new project, you recieve a category drop down to pick which category you want.

One of the categories is "Root" and I do not want this showing in the drop down list. I created my belongsTo method like so

project.php MODEL

var $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Category' => array(
        'className' => 'Category',
        'conditions' => array('Category.id '=>'1'),
        'fields' => '',
        'order' => ''
    ),
);

For my controller I have scaffolding turned on.

Here is my categories model

Category model

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
              开发者_如何学Python  //'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' => ''
        ),
    );
}


I assume you mean remove Root from the drop down menu cake produces with associations? In which case, try this:

$categories = $this->Category->find('list', 
                    array('conditions' => array('Category.name !=' => 'Root')));

$this->set(compact('categories'));


Use 'conditions' => array('Categories !=' =>'1'),, instead.

Validation is used in the saving of data, not in finding.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号