Tuesday, November 2, 2010

Re: find all with at least one associated model

The easiest way would be to Use countercache and then just add
post_count > 0 to conditions... if you cant zdf countercache for some
reason you could also inner join the posts Table, which would leave
all blogs that have no assocciated post...
On 1 Nov., 18:29, Max D <max.doerf...@googlemail.com> wrote:
> Hi,
>
> i use cake 1.3 and I have a "Blog hasMany Post" association and now I
> want to find all the Blogs, which have at least one Post. I use the
> Containable Behavior.
>
> I understand, that I could do a Blog->find('all', array('contain' =>
> array('Post'))) and simply filter the results for the Blogs, where the
> Post array is empty.
> But this doesn't work if I want to use pagination.
>
> I tried to paginate over the Post model:
> $this->paginate = array('Post' => array(
>         'contain' => array('Blog'),
>         'order' => 'Blog.created DESC',
>         'group' => 'Post.blog_id',
>         'limit' => 5,
> ));
> $blog = $this->paginate('Post');
>
> The Blog array looks good. I just get the Blogs with one Post each
> (which I don't actually need). But all the PaginatorHelper functions
> return wrong results.
> $this->Paginator->counter(array('model' => 'Post'));
> always returns 1, although there are 3 results in the $blogs array.
>
> Any ideas how I can achieve this?
> Thanks in advance
> Max

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

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: