Tuesday, May 4, 2010

Re: The docs for Containable does not work for complex search. Complex search is 2 models away. For cake 1.3

Sorry for the continuous replies.

Just realised that my scenario was not complete enough.

User hasOne Merchant (so Merchant belongsTo User and user_id inside
merchants table)
Shop hasMany Merchant (so Merchant belongsTo Shop and shop_id inside
merchants table)
Shop hasMany Domain (so Domain belongsTo Shop and shop_id inside
domains table)

i need to use Merchant model to do a find of all 4 related data.

i need to find the first datarow that matches conditions for Merchant,
User and Domain.

Merchant.owner = 1
User.email = abc@a.com
Domain.domain = http://abc.example.com
Domain.primary = 1


the datarow must contain data from all 4 models.


On May 4, 4:31 pm, AD7six <andydawso...@gmail.com> wrote:
> On May 4, 9:45 am, Kei Simone <kimc...@gmail.com> wrote:
>
> > Your statement on the right join i do not quite understand.
>
> > Which find statement are you explaining on?
>
> > Thank you for being enthusiastic to clear my doubts.
>
> Kei - you don't need to reply to each question individually - you can
> reply to many messages in the same reply ;)
>
> bindModel - if you bind a belongsTo or hasOne - is a join. so there is
> no advantage except it's a lot easier to say:
>
> $this->bindModel(array('hasOne' => array('OtherModel' => array(...));
> return $this->find(...);
> than
> return $this->find($type, $arraywithjoins);
>
> but it depends how explicit you want to be.
>
> Regarding "Your statement on right joins"
>
> These are not equivalent:
>
> return $this->find('all', array(
>                   'contain'=>array(
>                                          'Shop' => array('Domain'  =>
> array('conditions' => $someconditions)),
>                                         ),
>         ));
>
> return $this->find('all', array(
>                   'contain'=>array(
>                                          'Shop' => array('Domain')),
>                                         ),
>                  'conditions' => $someconditions
>         ));
>
> Let's say
>  $this is users and you've got 10k.
>  $someconditions = array('Domain.id' => 42)
>  you're expecting one result - the user linked via shop to the domain
> 42.
>
> The first query will return you 10k rows, and the domain data will be
> empty/missing for 9,999 rows - because cake builds the data using
> multiple queries and the first query has no conditions.
>
> The second query will return you 1 row - if you explicitly used joins
> or used one of the behaviors I mentioned that means you get what you
> asked for.
>
> If you're still in doubt - try it.
>
> hth,
>
> AD
>
> $this->
>
>
>
>
>
>
>
> > On May 3, 3:55 pm, AD7six <andydawso...@gmail.com> wrote:
>
> > > On May 2, 1:45 pm, Jon Bennett <jmbenn...@gmail.com> wrote:
>
> > > > > i need to use Merchant model to do a find of all 4 related data.
> > > > > notice that Domain is the only one NOT directly related to Merchant.
>
> > > > > i tried this in a method inside Merchant model:
>
> > > > > $this->Behaviors->attach('Containable');
> > > > > $this->User->Behaviors->attach('Containable');
> > > > > $this->Shop->Behaviors->attach('Containable');
> > > > > $this->Shop->Domain->Behaviors->attach('Containable');
>
> > > > > $this->recursive = -1;
> > > > > $this->User->recursive = -1;
> > > > > $this->Shop->recursive = -1;
> > > > > $this->Shop->Domain->recursive = -1;
>
> > > > > return $this->find('first',
> > > > >                array(
> > > > >                  'contain'=>array(
> > > > >                                         'User',
> > > > >                                         'Shop' => array('Domain' ),
>
> > > > >                                        ),
> > > > >                'conditions' => $conditions
> > > > >        ));
>
> > > > >http://book.cakephp.org/view/1323/Containablehaserrorsinterms of
> > > > > telling you where to place the conditions. my way is correct as of 1.3
> > > > > Cakephp
>
> > > > return $this->find('first', array(
> > > >         // conditions for the main model
> > > >         'conditions'=>array(),
> > > >         // contain
> > > >         'contain'=>array(
> > > >                 'User'=>array(
> > > >                         // conditions for the user model
> > > >                         'conditions'=>array('User.status'=>1),
> > > >                         'Other', 'Models', 'From', 'User'
> > > >                 ),
> > > >                 'Shop'=>array('Domain'),
> > > >         )
> > > > ));
>
> > > Using conditions like that is the equivalent of a right join - you're
> > > still going to get every single "main model" row - and just get an
> > > empty array for any row which links to a user with a status that isn't
> > > 1.
>
> > > hth,
>
> > > AD
>
> > > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelpothers 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 athttp://groups.google.com/group/cake-php?hl=en
>
> > Check out the new CakePHP Questions sitehttp://cakeqs.organdhelp 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 athttp://groups.google.com/group/cake-php?hl=en
>
> Check out the new CakePHP Questions sitehttp://cakeqs.organd 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 athttp://groups.google.com/group/cake-php?hl=en

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: