I am trying to find posts within a category that are 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 doesn’t seem to work. An error is showing this:
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’ve read that when you have HABTM between models, you should be able to retrieve it like so. However, the SQL shown doesn’t JOIN the categories 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'
)
);
}
This is the more efficient code: