Tuesday, February 28, 2012

Re: Filter on a deep model association

Your second proposition is close to what I need, but my Grail would be
to have the list of the authors (sorted alphabetically) each with its
associated books published in a given collection. I guess that with
some tweaking of the array of results I can get there but it's not
very elegant.
Thanks again.

On 28 fév, 23:16, lowpass <zijn.digi...@gmail.com> wrote:
> Go 3 levels deep so the data is arranged by the particular Collection
> you're after.
>
> $data = $this->Author->Book->Collection->find(
>         'first',
>         array(
>                 'conditions' => array(
>                         'Collection.id' = $c_id
>                 ),
>                 'contain' => array(
>                         'Book' => array(
>                                 'Author'
>                         )
>                 )
>         )
> );
>
> Because the Book model has the collection_id you really only need to
> go 2 models deep.
>
> $data = $this->Author->Book->find(
>         'all',
>         array(
>                 'conditions' => array(
>                         'Book.collection_id' = $c_id
>                 ),
>                 'contain' => array(
>                         'Collection',
>                         'Author'
>                 )
>         )
> );
>
> But then the data will be arranged badly. Collection, for instance,
> will be repeated for each Book.

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


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

No comments: