Just to clarify, the solution works for any association, we just use
hasOne to trick cake into building a JOIN query, but as you see in the
example we are using models from hasMany and HABTM.

On Sep 9, 1:21 pm, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]>
wrote:
> Sorry, I didn't mention that I use cakephp 1.1!
> I've read the post on teknoids blog. The disadvantage is, that this
> solution only works for hasOne associations and not for hasMany.
>
> But: I used the idea mentioned there and tried to create a LEFT JOIN
> in the sql-query.
> I found out, that this is possible with the 'finderQuery' value.
> My new associations in PostModel:
>
> var $hasMany = array(
>   'Comment' => array(
>     'className' => 'Comment',
>     'conditions' => '',
>     'foreignKey' => 'post_id',
>     'finderQuery' => '
>         SELECT * FROM comments
>         LEFT JOIN users ON (users.id = comments.user_id)
>         WHERE comments.post_id IN ({$__cakeID__$})'
>   )
> );
>
> return:
>
> array(
>   'Post' => array(...),
>   'Comment' => array(
>     'User' => array(...)
>   )
> )
>
> This works for me in 1.1 (don't know, if this is also possible in 1.2)
> Thanks for your help!
>
> derlippe
>
> On 9 Sep., 17:24, "Bernhard J. M. Grün"
>
> <[EMAIL PROTECTED]> wrote:
> > Hi,
>
> > This should be solvable with with Containable behavior. Or - if you want to
> > do this with just one query - use the infos you can find in teknoids 
> > blog:http://teknoid.wordpress.com/2008/07/17/forcing-an-sql-join-in-cakephp/
> > Those infos are also present in the manual for 1.2.
>
> > 2008/9/9 [EMAIL PROTECTED] <[EMAIL PROTECTED]>
>
> > > Hello newsgroup!
>
> > > I have defined the following associations in my models:
> > > PostModel:  "Post _hasMany_ Comments"
>
> > > var $hasMany = array(
> > >  'Comment' => array(
> > >    'className' => 'Comment',
> > >    'conditions' => '',
> > >    'foreignKey' => 'post_id',
> > >  )
> > > );
>
> > > CommentModel:  "Comment _belongsTo_ User"
>
> > > var $belongsTo = array(
> > >  'User' => array(
> > >    'className' => 'User',
> > >    'conditions' => '',
> > >    'foreignKey' => 'user_id'
> > >  )
> > > );
>
> > > When I use the query $this->Post->find(), cakephp only uses the
> > > associations in the PostModel (this is ok).
> > > This works for simple requests like $this->Post->find() and it returns
> > > the data array:
> > > array(
> > >  'Post' => array(...),
> > >  'Comment' => array(...)
> > > )
>
> > > But is it also possible to include the second association "Comment
> > > _belongsTo_ User" into the query $this->Post->find()?
> > > How do I have to define this or other associations in my Post-Model to
> > > get a data array like this:
> > > array(
> > >  'Post' => array(...),
> > >  'Comment' => array(
> > >    'User' => array(...)
> > >  )
> > > )
>
> > > I have tried many possibilities in the Post model... still the same
> > > results!
> > > The version I used now is a foreach(), which finds all Users for the
> > > returned Comments of $this->Post->find().
> > > ...this makes my CakeApp a little bit slow! Any ideas for a faster
> > > association?
>
> > > Many thanks for your answers!
>
> > > derlippe
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to