Sunday, May 30, 2010

Re: Deep associations

I suggest you take a look at the Containable behaviour in the CakePHP
manual at:
http://book.cakephp.org/view/1323/Containable

as well as how to join models at:
http://book.cakephp.org/view/1039/Associations-Linking-Models-Together#Joining-tables-1047

and last but maybe more important, how to use complex find conditions,
at:
http://book.cakephp.org/view/1017/Retrieving-Your-Data#Complex-Find-Conditions-1030

The above information should give you the necessary tools to implement
your count of items per user.
Enjoy,
John

On May 30, 7:44 am, azv <a...@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

No comments: