1

我正在使用下面的代码,但 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',
    )
);
4

1 回答 1

0

首先你不应该使用

'递归' => 1
具有可包含的行为。此行为是根据需要获取相关模型,因此在您的 AppModel 类中添加
公共 $recursive = 0
. 接下来在您的控制器中尝试:

$this->paginate['User'] = array(
            'conditions' => $conditions,
            'limit' => 10, 
            'contain' => array(
                        'Comment' => array(
                            'limit' => 1,
                            'User' => array(
                                'fields' => array('username','id'),
                            ),
                        )
            ),
);
于 2013-10-15T07:51:05.497 回答