Skip to content
This repository has been archived by the owner on Feb 25, 2020. It is now read-only.

hasOne not automatically bound when calling $Model::find('list'); #4

Open
cdvrooman opened this issue Mar 15, 2010 · 5 comments
Open

Comments

@cdvrooman
Copy link

I'm trying to do this:

$this->Group->find('list');

and my SQL is coming out like this:

SELECT Group.id, Group.name, GroupPermission.* FROM groups AS Group WHERE 1 = 1

In permisssionable/models/behaviors/permissionable.php, when I debugged the $Model before and after this line of the beforeFind() function:

$this->_bind($Model, array( ... ));

the $Model did not and then did have a 'hasOne' binding to GroupPermission however for some reason it isn't being applied to the ->find('list') query.

I tried to do a $this->Group->find('all'); and that did work as expected.

I'm calling $this->Group from my users_controller where I have:

var $uses = array('User', 'Group');

I have User HABTM Group and Group HABTM User defined in the appropriate model files.

@cdvrooman
Copy link
Author

I did some poking around in /cake/libs/model/model.php and in the _findList() function, I found this:
if (!isset($query['recursive']) || $query['recursive'] === null) {
$query['recursive'] = -1;
}
Which could definitely be screwing things up if 'recursive' is not being explicitly set by Permissionable', perhaps only when the find is of the 'list' variety?

@cdvrooman
Copy link
Author

On a related note, I'm running into a similar problem when trying to save something to the Group table, where first the ability to write to the table is verified. The SQL is appearing as:
SELECT COUNT() AS count, GroupPermission. FROM groups AS Group WHERE Group.name = 'Root' AND Group.id != ' ... uuid ...'

In \models\behaviors\permissionable.php I tried adding
'recursive' => 1
to hasPermissions() here:
$perm = $Model->{$alias}->find('count', array( ... ));
but it didn't have any effect.

I also tried modifying $queryData['recursive'] directly in the beforeFind() function, but that caused my browser to be unable to resolve the page.

@cdvrooman
Copy link
Author

Two alternatives are:

  1. Adding 'permissionable' => false to the find parameters, i.e.:
    $users = $this->User->find('list', array(
    'permissionable' => false,
    'conditions' => array('User.name <>' => 'guest')
    ));
    or, using find('all') but specifying the fields, i.e.
    $users = $this->User->find('all', array(
    'recursive' => -1,
    'conditions' => array('User.name <>' => 'guest'),
    'fields' => array('User.id', 'User.name')
    ));

@cdvrooman
Copy link
Author

Of course in the second case you would have to use Set::extract() or something similar on the results to leave only the 'user_id' => 'user_name'.

@afrance
Copy link

afrance commented Feb 9, 2011

Any real solution to this? The same issue crops up when attempting to use the tree behavior.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants