Saturday, May 29, 2010

Re: Deep associations

By the looks of it you named your model correctly as "Items" but in your conditions your are referencing "Item". 

To find "count" ... 

$this->Section->find('count' , array(
                                                  'conditions'  => array(
                                                                                 'Items.user_id' => 1 
                                                                                 )
                                                   )
                             );

- Ed 

On Sun, May 30, 2010 at 12:44 AM, azv <aviv@keebali.com> wrote:


I'm playing with a simple model where

Section hasMany Catalogitem hasMany Item belongsTo User

and naturaly the other way-

User hasMany Item belongsTo Catalogitem belongsTo Section.

I'm trying to get count how many items I have per section for
user.id=1 - which is a very simple sql query (with 2 joins)-

SELECT sections.id, count(*)
FROM sections
JOIN catalogitems ON sections.id=catalogitems.section_id
JOIN items ON catalogitems.id=items.catalogitem_id
WHERE user.id=1
GROUP BY sections.id

But I'm not sure how to use cake for that:

$this->Section->find('all' , array( 'conditions' =>
array('Item.user_id' => 1)) )

I'm getting an error that 'Item.user_id' is an Unknown column.

Can anyone shed light regarding what's the problem, and how do I get
the find() to return a count of items per section for user.id?

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

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: