CakeDc用户$ hasMany没有返回关联模型

时间:2014-08-02 22:50:17

标签: cakephp cakephp-2.5

我正在使用CakeDc Users插件2.0,在插件admin_index视图中,我希望为用户看到相关的图书。

我已将$ hasMany添加到User.php模型

public $hasMany = array(
    'Book' => array(
        'className' => 'Book',
        'foreignKey' => 'user_id',
        'dependent' => true,
        'conditions' => '',
        'fields' => '',
        'order' => 'order',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

在UserController.php中我添加了'contains'

protected function _setupAdminPagination() {
    $this->Paginator->settings = array(
        'limit' => 20,
        'order' => array(
            $this->modelClass . '.created' => 'desc'),
        'contain' => array('Book')
    );
}

public function admin_index() {
    $this->Prg->commonProcess();
    unset($this->{$this->modelClass}->validate['username']);
    unset($this->{$this->modelClass}->validate['email']);
    $this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs;

    if ($this->{$this->modelClass}->Behaviors->loaded('Searchable')) {
        $parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs);
    } else {
        $parsedConditions = array();
    }
    $this->_setupAdminPagination();
    $this->Paginator->settings[$this->modelClass]['conditions'] = $parsedConditions;
    $this->set('usersList', $this->Paginator->paginate());
    $this->layout = 'btst';
}

但是我没有在视图中获得相关的书籍,我仍然只是一组用户。这应该有用吗?

更新:

debug ($this->Paginator->settings);

输出

array(
    'limit' => (int) 20,
    'order' => array(
         'User.created' => 'desc'
    ),
    'contain' => array(
         (int) 0 => 'Book'
    ),
    'User' => array(
         'conditions' => array()
    )
)

1 个答案:

答案 0 :(得分:1)

调试$ this-> Paginator->设置,然后调用$ this-> Paginator并尝试

'contain' => array('Book')

然而,直接修改插件是不好的做法,在最糟糕的情况下,您将无法再顺利更新它们(合并冲突,API更改......)。插件文档附带了大量文档,解释了如何扩展插件而不是直接修改插件。

修改:这实际上是插件中的一个错误,已修复为https://github.com/cakedc/users/commit/73aa350