this is my situation, I have a Post model that can belong to multiple Categories. This I figured out how to do:
class Post extends AppModel
{
var $hasAndBelongsToMany = array('Category');
}
And the Category model:
class Category extends AppModel
{
var $hasAndBelongsToMany = array ('Post');
}
Now, this all works fine and all that. But I just cannot figure out how to search for posts that belong to a certain category, for example “News” with the id of 1 to use in:
class CategoriesController extends AppController {
function view ($id = 0) {
// doesnt work
$this->Category->Video->find ('all', array('conditions' => array('category_id' => $id));
}
}
I’ve been trying to find an answer on Google and the CookBook but have found nothing yet. Does anyone know how this can be achieved?
Thank you.
You are performing a find on Video model, when you showed association on Category and Post models, not Video. Assuming this is a typo error:
If you want to search, as you stated, posts that are of “News” category do in posts_controller.php:
Or in the categories_controller.php