0

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()
    )
)
Keith Power
  • 13,891
  • 22
  • 66
  • 135

1 Answers1

1

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

floriank
  • 25,546
  • 9
  • 42
  • 66
  • Yes I have extended the model and controller. The docs have a good explanation for this. But since I was not getting the desired result I thought directly changing the code might show where I was going wrong. I have updated my code but I still only get the User array. I fully intend on going back to the extended files. Thanks – Keith Power Aug 03 '14 at 15:14
  • See the 'User' key in the paginator array. Unset it for testing before calling paginate(), I think this might be a bug in the plugin. I guess what happens is the settings from use 'User' array are taken and that has nothing except the empty conditions. – floriank Aug 03 '14 at 16:41
  • I get a database error when I try unset($this->Paginator->settings['User']); - Column not found: 1054 Unknown column 'order' in 'order clause' – Keith Power Aug 03 '14 at 19:10
  • In _setupAdminPagination() change settings = array( to settings = array('User' => array(. I guess you know what I mean. Then try again. – floriank Aug 03 '14 at 20:21
  • I fixed this in the plugin itself. No need for you to make this change. Review the latest versions changelog.md. Fixed in https://github.com/cakedc/users/commit/73aa350 – floriank Aug 04 '14 at 13:01
  • I added what you suggested settings = array('User' => array( etc and I got the same sql error. This code should have definitely worked. So I looked at the error closely and spotted that the error was for the book query and Unknown column 'order' which I don't have. If I took out the contain part it worked again. I checked my $hasMany and saw 'order' => 'order', which should not be there. It is for another model that I have. I have removed this and now the contain works. Both your suggestions now work. I have gone back to your original. Thanks. – Keith Power Aug 04 '14 at 13:06
  • sorry, didn't mention that it worked with the unset User so your fix now eliminates this. :-) – Keith Power Aug 04 '14 at 13:17
  • Welcome, at least it works now and I could fix a bug. So win-win for everyone! :) – floriank Aug 04 '14 at 13:53