Monday, May 30, 2011

Find with $hasAndBelongsToMany

I have three models 
Authors 
Books (hasandbelongstomany category)
Category

They are properly configured for $hasAndBelongsToMany (books/category) relationship.

I need to get all books by an author in a certain category (5 in this example)

Obviously the category id is not in the authors table so I am trying to figure out how to reference the category id.  I have tried below to no avail.

$this->Category->Author->find('all', array('conditions'=>array('Category.id'=>'5', 'Author.id'=>'2')));
or 
$this->Author->find('all', array('conditions'=>array('Category.id'=>'5', 'Author.id'=>'2')));

This is the first time I have done a HABTM query.  (searched manual and and a couple cake books - no help)

Thanks!

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
 
 
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

No comments: