"group by" to work would do what you want here. You can force a "group
by" by adding a 'conditions' key with a value of "where 1=1 group by
Post.id" in the contained model array, but you'll get an error. The
reason for this is Containable will use multiple queries instead of
one, so you cannot refer to the original model in the contained models
(the group by will refer to a model/table not referenced in the second
query).
One sort of "non-Cakey" way to do it is to force Cake to do left/inner/
right joins by:
$this->find('all', array(
'conditions' => $conditions,
'fields' => $fields + array('SUM(Vote.rank)/COUNT(Vote.id) AS
rank'),
'joins' => array('INNER JOIN votes AS Vote ON Vote.post_id =
Post.id'),
'group' => 'Post.id',
'page' => $page,
'limit' => $limit,
'order' => $order,
'contain' => array(
'Author' => array( 'fields' => array(' Author.name,
Author.surname' ))
)
));
Now, you may wish to change the inner join to left join or what have
you, but you get the idea. The non-Cakey part is having to manually
write the SQL join(s).
Another option would be to try out the Linkable Behavior, which
accomplishes the same thing but automagically:
http://rafaelbandeira3.wordpress.com/2008/11/16/linkable-behavior-taking-it-easy-in-your-db/
--~--~---------~--~----~------------~-------~--~----~
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:
Post a Comment