0

我正在使用 CakeDc 用户插件 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 我添加了“包含”

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()
    )
)
4

1 回答 1

1

在调用 $this->Paginator 之前调试 $this->Paginator->settings 并尝试

'contain' => array('Book')

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

Edit: This was actually a bug in the plugin, fixed it in https://github.com/cakedc/users/commit/73aa350

于 2014-08-03T12:47:01.917 回答