Monday, May 3, 2010

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

On May 3, 7: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

Containable isn't designed or suitable for that sort of thing.

Try the linkable behavior of my own onequery. Know that both of these
behaviors are not doing anything you can't achieve yourself using bind
model (see habtm docs in the book and see the Filter example). The
key, always, to "complex" queries (IMO) is to write the sql first and
then simply bindModel('belongsTo .... for each join you want to
achieve.

hth,

AD
http://rafaelbandeira3.wordpress.com/2008/11/16/linkable-behavior-taking-it-easy-in-your-db/
http://github.com/AD7six/mi/blob/master/models/behaviors/one_query.php

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: