i have the following for my cake 1.3:

User hasOne Merchant (so Merchant belongsTo User)
Shop hasMany Merchant (so Merchant belongsTo Shop as well.)
Shop hasMany Domain (so Domain belongsTo Shop.)

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 = a...@a.com
Domain.domain = http://abc.example.com
Domain.primary = 1


the datarow must contain data from all 4 models.

the find must be done inside the Merchant model. as in $this->find...

Love to hear more from you about Containable. Really interested to see
how powerful Containable is.


On May 3, 3:33 pm, Andrei Mita <andrei.m...@gmail.com> wrote:
> Simone,
>
> Containable is extremely powerful. I am just amazed how little time it takes
> me to write complex queries.
>
> Give me the structure of the tables and an example of what do you want to
> retrieve from them and I might be able to help you around.
>
>
>
> On Mon, May 3, 2010 at 8:02 AM, Kei Simone <kimc...@gmail.com> wrote:
> > Hi i did this
>
> > $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(
> >                         // conditions for the main model
> >                         'conditions'=>array('Merchant.owner'=>1),
> >                         // contain
> >                        'contain'=>array(
> >                                'User'=>array(
> >                                        // conditions for the user model
>
> >  'conditions'=>array('User.group_id'=>MERCHANTS,
> >                                                            'User.email' =>
> > 'a...@a.com',),
>
> >                                ),
> >                                'Shop'=>array(
>
> >  'conditions'=>array('Shop.web_address' => 'http://
> > abc.myspree2shop.com',),
> >                                              'Domain'),
> >                        )
> >                ));
>
> > i got this in the sql query log
>
> > SELECT `Merchant`.`id`, `Merchant`.`owner`, `Merchant`.`shop_id`,
> > `Merchant`.`user_id`, `Shop`.`id`, `Shop`.`theme_id`, `Shop`.`name`,
> > `Shop`.`web_address`, `Shop`.`created_on`, `Shop`.`modified_on`,
> > `Shop`.`status`, `User`.`id`, `User`.`email`, `User`.`password`,
> > `User`.`group_id`, `User`.`full_name`, `User`.`name_to_call`,
> > `User`.`last_login_on`, `User`.`status`, `User`.`created_on`,
> > `User`.`modified_on` FROM `merchants` AS `Merchant` LEFT JOIN `shops`
> > AS `Shop` ON (`Merchant`.`shop_id` = `Shop`.`id` AND
> > `Shop`.`web_address` = 'http://abc.myspree2shop.com') LEFT JOIN
> > `users` AS `User` ON (`Merchant`.`user_id` = `User`.`id` AND
> > `User`.`group_id` = 3 AND `User`.`email` = 'a...@a.com') WHERE
> > `Merchant`.`owner` = 1 LIMIT 1
>
> > SELECT `Shop`.`id`, `Shop`.`theme_id`, `Shop`.`name`,
> > `Shop`.`web_address`, `Shop`.`created_on`, `Shop`.`modified_on`,
> > `Shop`.`status` FROM `shops` AS `Shop` WHERE `Shop`.`id` = 1 AND
> > `Shop`.`web_address` = 'http://abc.myspree2shop.com'
>
> > SELECT `User`.`id`, `User`.`email`, `User`.`password`,
> > `User`.`group_id`, `User`.`full_name`, `User`.`name_to_call`,
> > `User`.`last_login_on`, `User`.`status`, `User`.`created_on`,
> > `User`.`modified_on` FROM `users` AS `User` WHERE `User`.`id` = 1 AND
> > `User`.`group_id` = 3 AND `User`.`email` = 'a...@a.com'
>
> > i got this result
>
> > Array
> > (
> >    [Merchant] => Array
> >        (
> >            [id] => 1
> >            [owner] => 1
> >            [shop_id] => 1
> >            [user_id] => 1
> >        )
>
> >    [Shop] => Array
> >        (
> >            [id] =>
> >            [theme_id] =>
> >            [name] =>
> >            [web_address] =>
> >            [created_on] =>
> >            [modified_on] =>
> >            [status] =>
> >        )
>
> >    [User] => Array
> >        (
> >            [id] =>
> >            [email] =>
> >            [password] =>
> >            [group_id] =>
> >            [full_name] =>
> >            [name_to_call] =>
> >            [last_login_on] =>
> >            [status] =>
> >            [created_on] =>
> >            [modified_on] =>
> >        )
>
> > )
>
> > When i changed your recommendation to
>
> >                            return $this->find('first',
> >                                        array(
> >                                          'contain'=>array(
> >                                                           'User',
> >                                                           'Shop' =>
> > array('Domain'
> >                                                                        ),
>
> >                                                           ),
> >                                           'conditions' =>
> > array('User.group_id'=>MERCHANTS,
> >                                                            'User.email' =>
> > 'a...@a.com',
>
> > 'Shop.web_address' => 'http://abc.myspree2shop.com',
> >                                                           )
> >                                          )
> >                                );
>
> > all i did was to move all the conditions to the main model.
>
> > i got this in my sql query log:
> > SELECT `Merchant`.`id`, `Merchant`.`owner`, `Merchant`.`shop_id`,
> > `Merchant`.`user_id`, `Shop`.`id`, `Shop`.`theme_id`, `Shop`.`name`,
> > `Shop`.`web_address`, `Shop`.`created_on`, `Shop`.`modified_on`,
> > `Shop`.`status`, `User`.`id`, `User`.`email`, `User`.`password`,
> > `User`.`group_id`, `User`.`full_name`, `User`.`name_to_call`,
> > `User`.`last_login_on`, `User`.`status`, `User`.`created_on`,
> > `User`.`modified_on` FROM `merchants` AS `Merchant` LEFT JOIN `shops`
> > AS `Shop` ON (`Merchant`.`shop_id` = `Shop`.`id`) LEFT JOIN `users` AS
> > `User` ON (`Merchant`.`user_id` = `User`.`id`) WHERE `User`.`group_id`
> > = 3 AND `User`.`email` = 'a...@a.com' AND `Shop`.`web_address` =
> > 'http://abc.myspree2shop.com'LIMIT 1
>
> > SELECT `Shop`.`id`, `Shop`.`theme_id`, `Shop`.`name`,
> > `Shop`.`web_address`, `Shop`.`created_on`, `Shop`.`modified_on`,
> > `Shop`.`status` FROM `shops` AS `Shop` WHERE `Shop`.`id` = 2
>
> > SELECT `Domain`.`id`, `Domain`.`domain`, `Domain`.`shop_id`,
> > `Domain`.`primary`, `Domain`.`always_redirect_here` FROM `domains` AS
> > `Domain` WHERE `Domain`.`shop_id` = (2)
>
> > i got this in my result:
>
> > Array
> > (
> >    [Merchant] => Array
> >        (
> >            [id] => 2
> >            [owner] => 1
> >            [shop_id] => 2
> >            [user_id] => 2
> >        )
>
> >    [Shop] => Array
> >        (
> >            [id] => 2
> >            [theme_id] => 0
> >            [name] =>
> >            [web_address] =>http://abc.myspree2shop.com
> >            [created_on] => 2010-05-03 04:36:08
> >            [modified_on] => 0000-00-00 00:00:00
> >            [status] => 1
> >            [Domain] => Array
> >                (
> >                    [0] => Array
> >                        (
> >                            [id] => 2
> >                            [domain] =>http://abc.myspree2shop.com
> >                            [shop_id] => 2
> >                            [primary] => 1
> >                            [always_redirect_here] => 0
> >                        )
>
> >                )
>
> >        )
>
> >    [User] => Array
> >        (
> >            [id] => 2
> >            [email] => a...@a.com
> >            [password] => password
> >            [group_id] => 3
> >            [full_name] => ally
> >            [name_to_call] => ally
> >            [last_login_on] =>
> >            [status] => 1
> >            [created_on] => 2010-05-03 04:36:08
> >            [modified_on] => 0000-00-00 00:00:00
> >        )
>
> > )
>
> > Something is wrong here. i think the document is missing something. i
> > cannot be sure.
>
> > I have to say that i CANNOT use Containable the moment i need to set
> > conditions for the models that are 2 steps away from the main model.
> > which is what i am looking for.
>
> > I am willing to admit i make mistakes since i am only using cake for 1
> > month so far. but i think someone has to show me where i made it
> > because i really cannot see it.
>
> > I have to say that containable can be quite powerful and i tried to go
> > through the code. but i just cannot follow it so i am still confused.
>
> > Again, for those who have similar situations, please use joins. i am
> > keeping this thread alive, because i REALLY want to learn how to use
> > Containable powerfully.
>
> > Jon, thanks for your reply.
>
> > Would appreciate it if you can follow up with more explanations.
>
> > I am VERY VERY eager to learn more.
>
> > On May 2, 7: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/Containablehaserrors in terms 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'),
> > >         )
> > > ));
>
> > > hth
>
> > > Jon
>
> > > --
> > > jon bennett...
>
> read more »

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to