0
votes

I am using the CakeDc Users plugin 2.0 and on the plugins admin_index view I would like to see the associated Books for the users.

I have added $hasMany to User.php Model

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

And in the UserController.php I added 'contain'

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';
}

However I do not get the associated books in the view, I still just the array of users. This should work?

UPDATE:

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

Output

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

1 Answers

1
votes

Debug $this->Paginator->settings before calling $this->Paginator and try

'contain' => array('Book')

However it is bad practice to modify plugins directly, you won't be able to update them anymore without trouble (merge conflicts, API changes...) in the worst case. The plugins documentation comes with a lot documentation that explains how to extend the plugin instead of modinfy it directly.

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