Thursday, July 2, 2009

count + habtm + paginate

Hello group,

I have got a problem with the count of habtm entries and pagination:

We got the usual models Posts and Tags that are associated with habtm.
What I want to achieve is, to get all Tags and the count of
association to the Post. After a moment of searching I found a
solution for that by creating an association on the fly:

$this->Tag->bindModel(array('hasOne'=>array('PostsTag')), array(),
true);
$this->paginate = array(
'limit' => 25,
'order' => 'Tag.name ASC',
'fields' => array(
'Tag.id',
'Tag.name',
'COUNT(PostsTag.tag_id) as `count`'
),
'group' => array('Tag.id')
);

It took me a while to figure out that the 3rd parameter of bindModel
isn't true by default.. The resulting array looks like this:

Array
(
[0] => Array
(
[Tag] => Array
(
[id] => 7
[name] => test 1
)

[0] => Array
(
[count] => 1
)

)

[1] => Array
(
[Tag] => Array
(
[id] => 11
[name] => test 2
)

[0] => Array
(
[count] => 3
)

)

So, almost there. The problem I have now, is that i want to paginate
the results, with the option to order the results by count. I tried
$paginate->sort('Count', '0.count'); in the view, which doesn't work..

thanks for any hints..
harpax


--~--~---------~--~----~------------~-------~--~----~
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: