I'm getting a lot of extra queries when doing certain contains.
Cakeversion: 1.2.0.7962

I've got three Models

Member
Item
Itemrating

Itemrating belongsTo Item
Item BelongsTo Member

I'm trying to do a find on Itemrating and contain Item and Member

Here is the code to do the find:
                $this->Itemrating->contain(array('Item'=>array('fields'=>array
('id','slug','member_id'),'Member'=>array('fields'=>array
('id','username','slug','gender_id','birth')))));
$data = $this->Itemrating->find('all',array('conditions'=>array
('date'=>date('Y-m-d'),'Item.type_id'=>1, 'Itemrating.votes
>'=>0),'order'=>array('Itemrating.rating'=>'DESC'),'limit'=>20));

It should be possible for cake to get all this info in one query. But
instead I get 1 query that gets the data from itemratings and member,
then for each result from that query, two additional queries are
generated, one to get the member data, and one to get the item data.
Is this a bug? Or am I doing something wrong?


Here are the queries being built:

Nr      Query   Error   Affected        Num. rows       Took (ms)
1       SELECT `Itemrating`.`id`, `Itemrating`.`item_id`,
`Itemrating`.`date`, `Itemrating`.`votes`, `Itemrating`.`rating`,
`Item`.`id`, `Item`.`slug`, `Item`.`member_id` FROM `itemratings` AS
`Itemrating` LEFT JOIN `items` AS `Item` ON (`Itemrating`.`item_id` =
`Item`.`id`) WHERE `date` = '2009-02-24' AND `Item`.`type_id` = 1 AND
`Itemrating`.`votes` > 0 ORDER BY `Itemrating`.`rating` DESC LIMIT 20
3       3       10
2       SELECT `Item`.`id`, `Item`.`slug`, `Item`.`member_id` FROM `items`
AS `Item` WHERE `Item`.`id` = 3                 1       1       0
3       SELECT `Member`.`id`, `Member`.`username`, `Member`.`slug`,
`Member`.`gender_id`, `Member`.`birth` FROM `members` AS `Member`
WHERE `Member`.`id` = 2                 1       1       0
4       SELECT `Item`.`id`, `Item`.`slug`, `Item`.`member_id` FROM `items`
AS `Item` WHERE `Item`.`id` = 253               1       1       0
5       SELECT `Member`.`id`, `Member`.`username`, `Member`.`slug`,
`Member`.`gender_id`, `Member`.`birth` FROM `members` AS `Member`
WHERE `Member`.`id` = 2                 1       1       0
6       SELECT `Item`.`id`, `Item`.`slug`, `Item`.`member_id` FROM `items`
AS `Item` WHERE `Item`.`id` = 1                 1       1       0
7       SELECT `Member`.`id`, `Member`.`username`, `Member`.`slug`,
`Member`.`gender_id`, `Member`.`birth` FROM `members` AS `Member`
WHERE `Member`.`id` = 2                 1       1       0

--~--~---------~--~----~------------~-------~--~----~
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