Say I have a model Post
and a model Comment
related as follows:
Post hasMany Comment
Comment belongsTo Post
How do use find('all')
to retrieve every Post
with its associated latest Comment
?
I have tried defining a hasOne
relationship in Post
as:
var $hasOne = array('LatestComment' => array('className' => 'Comment', 'order' => 'LatestComment.created DESC'));
But when I do a Post->find('all')
it returns every Post
multiple times, once per each Comment
, with LatestComment
set to the different Comments
.
To remove the duplicates you want to use: GROUP BY in your find all query. I believe Cake has a 'group' => option as well.