Thursday, December 27, 2012

Re: using group by and order by together

I'm not sure what the problem is but this code should get you what you want:


public function getCategoriesWithLatest()
{
return $this->Category->find(
'all',
array(
'fields' => array(
'Category.id',
'Category.name'
),
'contain' => array(
'Post' => array(
'fields' => array(
'Post.id',
'Post.title'
),
'limit' => 1,
'order' => array(
'Post.created' => 'DESC'
)
)
)
)
);
}

Put it in the Post model.

On Wed, Dec 26, 2012 at 9:18 PM, Fernando Queiroz <facquesta5@gmail.com> wrote:
> Hello friends,
>
> I have a doubt, I am working with two tables categories and posts on a given
> query want to display in my home only 1 post from each category, the last.
> As if it was the home of a forum.
>
> I am able to display one post from each category, but they can not be the
> last registered.
>
> Follow my code using group by and order by together
> http://bin.cakephp.org/saved/84693
>
> Does anyone by chance has gone through this problem?
>
> Thank you in advance!
>
>
> --
> Like Us on FaceBook https://www.facebook.com/CakePHP
> Find us on Twitter http://twitter.com/CakePHP
>
> ---
> 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.
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>
>

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
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.
Visit this group at http://groups.google.com/group/cake-php?hl=en.

No comments: