I am trying to find posts in a category associated with a category. Right now I have this:
$this->set('posts', $this->Category->Post->find('all', array('conditions' => array('Category.uri' => $uri))));
But this does not seem to work. This is indicated by an error:
Warning (512): SQL Error: 1054: Unknown column 'Category.uri' in 'where clause' [CORE/cake/libs/model/datasources/dbo_source.php, line 684] ..<snipped>... Query: SELECT `Post`.`id`, `Post`.`title`, `Post`.`uri`, `Post`.`body`, `Post`.`created`, `Post`.`modified` FROM `posts` AS `Post` WHERE `Category`.`uri` = 'holidays'.
I read that when you have a HABTM between models, you can get it like this. However, the SQL shown does not connect to the category table.
// Category Model class Category extends AppModel { var $name = 'Category'; var $hasAndBelongsToMany = array( 'Post' => array( 'className' => 'Post' ) ); } // Post Model class Post extends AppModel { var $name = 'Post'; var $hasAndBelongsToMany = array( 'Category' => array( 'className' => 'Category' ) ); }
source share