On Fri, Oct 15, 2010 at 2:39 AM, brybot <[email protected]> wrote:
> I've searched and looked through this group and the online
> documentation for hours, but I'm having some difficulty with a simple
> problem.
>
> Its a simple setup:
>
> User HABTM Permissions
>
> Everything is setup properly (3 tables: users, users_permissions,
> permissions). And everything works fine, I'm just trying to make a
> simple select.
>
> I want a list of the permission for a certain user.
>
> I can get a list of all permission: $this->Permission->find('list',
> array('fields'=>'id'))
>
> but i cannot seem to find the proper syntax to limit it to one user.
> Everytime I try to limit the results by the user as the HABTM cake
> documentation says:
>
> $this->Permission->User->findById('4',
> array('fields'=>'Permission.*')) or
> $this->Permission->User->find('all',
> array('conditions'=>array('User.id'=>'4'),
> 'fields'=>array('Permission.*')))
>
> The generated query drops the inner join to the permission table, and
> only selects the user.
>
> Or I can get a list of the User and all its Permissions in a rather
> large array, but I cannot get a list.
>
You want just a single User so you should use 'first' not 'all'.
$this->Permission->User->find(
'first',
array(
'conditions'=>array(
'User.id'=> $id
)
)
);
If you're associations are set up correctly, that should pull in the
User's Permissions, also.
Personally, I prefer to use Containable. Because I use it a lot, I
declare it in AppModel::actsAs array.
$this->Permission->User->find(
'first',
array(
'conditions'=>array(
'User.id'=> $id
),
'contain' => array(
'Permission' => array(
'fields' => array(
'Permission.id',
'Permission.name'
)
)
)
)
);
Or, you could declare the HABTM assoc with a 'with' model[1] and run
the find on that. But I suppose that would give you just the
Permission.id and not the name (or whatever column you're using. But
maybe that's all you want. Or, if you use the above code, you could
then use Set::extract() to create your list.
[1]
http://marianoiglesias.com.ar/cakephp/modelizing-habtm-join-tables-in-cakephp-1-2-with-and-auto-with-models/
Check out the new CakePHP Questions site http://cakeqs.org and help others with
their CakePHP related questions.
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en