Tuesday, September 29, 2009

Re: Containable, Pagination and HABTM

Hi!

Did you solve your problem?

I think you have a typo in the conditions element:

$this->paginate['Group'] = array(
'order' => 'Group.name
ASC',
'contain' => array(
'User' => array
(

'conditions' => array(

'User.id =' => $this->Auth->user('id')
)
)
)
);

Note the equal sign after 'User.id

Nevertheless, I dont think that this fix will solve your problem. I
need to do something like this, and the paginate function returns all
the groups, even those that haven't the user I requested for.

On 12 ago, 14:49, sunertl <sune...@gmail.com> wrote:
> Hey there,
> after hours of googling and reading tutorials I am desperate now and
> seek your help.
>
> I am working with Cake 1.2.4.8284, I have two models, User (table
> users) and Group (table group) where User HABTM Group. The join table
> is called groups_users, it contains user_id and group_id and an
> additional field called admin.
>
> I try to find all all groups a user is member of and I use paginate
> for it:
> $this->paginate['Group'] = array(
>                                                 'order' => 'Group.name ASC',
>                                                 'contain' => array(
>                                                         'User' => array(
>                                                                 'conditions' => array(
>                                                                         'User.id' => $this->Auth->user('id')
>                                                                 )
>                                                         )
>                                                 )
>                                         );
> $groups = $this->paginate('Group');
> $this->set('groups', $groups);
>
> When I use debug($groups) I get the following (wrong) result:
> [0] => Array
>         (
>             [Group] => Array
>                 (
>                     [id] => 1
>                     [name] => test
>                 )
>
>             [User] => Array
>                 (
>                     [0] => Array
>                         (
>                             [id] => 1
>                             [name] => Administrator
>                             [GroupsUser] => Array
>                                 (
>                                     [id] => 1
>                                     [admin] => 1
>                                     [group_id] => 1
>                                     [user_id] => 1
>                                 )
>
>                         )
>
>                 )
>
>         )
>
>     [1] => Array
>         (
>             [Group] => Array
>                 (
>                     [id] => 2
>                     [name] => test
>                 )
>
>             [User] => Array
>                 (
>                 )
>         )
>
> Ok, I thought, maybe the condition in the contain-clause is wrong but
> then I saw the SQL-debug in the view:
> [...]
> 13      SELECT COUNT(*) AS `count` FROM `groups` AS `Group` WHERE 1 = 1         1
> 1       0
> 14      SELECT `Group`.`id`, `Group`.`name` FROM `groups` AS `Group` WHERE
> 1 = 1 ORDER BY `Group`.`name` ASC LIMIT 20              2       2       0
> 15      SELECT `User`.`id`, `User`.`name`, `GroupsUser`.`id`,
> `GroupsUser`.`admin`, `GroupsUser`.`group_id`, `GroupsUser`.`user_id`
> FROM `users` AS `User` JOIN `groups_users` AS `GroupsUser` ON
> (`GroupsUser`.`group_id` IN (1, 2) AND `GroupsUser`.`user_id` =
> `User`.`id`) WHERE `User`.`id` = 1              1       1       0
>
> So it looks like everything works correctly but the result of the last
> SQL statement is not saved into $groups.
>
> Does anybody have a clue where I make the mistake? I tried almost each
> and every tutorial and how-to but no combination of arguments seems to
> work.
>
> I am working with Cake for almost three years now so I won't consider
> myself a newbie but I have no idea what goes wrong here...
>
> Thanks a lot in advance!

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: