CakePHP模型在使用Containable时不关联

时间:2013-09-14 06:31:47

标签: cakephp paginator containable

我正在使用下面的代码,但Cakephp出错:“模型”评论“与模型”用户“无关”

$this->Paginator->settings = array(
            'contain' => array_merge(
                    array(
                        'Comment' => array(
                            'limit' => 1,
                            'User' => array(
                                'fields' => array('username','id'),
                            ),
                        )
                    ),
            ),
            'recursive' => 1,
            'conditions' => $conditions,
            'limit' => 10,                    
    );

    $posts = $this->Paginator->paginate('Post');

在用户模型中:

public $hasMany = array(
     'Comment' => array(
        'className' => 'Comment',
        'foreignKey' => 'author',
        'dependent' => false
    ),
);

在评论模型中:

    public $belongsTo = array(
    'User' => array(
        'className' => 'User',
        'foreignKey' => 'author',
    )
);

1 个答案:

答案 0 :(得分:0)

首先,您不应将

'recursive' => 1
与可包含行为一起使用。此行为是为了获得所需的相关模型,因此在AppModel类中添加
public $recursive = 0
。在控制器中接下来尝试:

$this->paginate['User'] = array(
            'conditions' => $conditions,
            'limit' => 10, 
            'contain' => array(
                        'Comment' => array(
                            'limit' => 1,
                            'User' => array(
                                'fields' => array('username','id'),
                            ),
                        )
            ),
);