Нет простого способа сделать это, но это может быть достигнуто несколькими различными способами. Вот, пожалуй, самый простой: определить функцию, которая извлекает список идентификаторов рецептов из таблицы соединений, а затем выполняет поиск по ней.
Итак, по модели Ингредиент:
function getRecipesFromIngredients($ingredient_ids) {
// IngredientsRecipe is the automatically created model
// Use what you defined in the 'with' key on your HABTM
// definition if you defined a 'with' key
$results = $this->IngredientsRecipe->find('all', array(
'conditions' => array(
'ingredient_id' => $ingredient_ids
),
));
return Set::extract('/IngredientsRecipe/recipe_id', $results);
}
Это становится очень проверяемой, многократно используемой функцией.
Затем в рецепте контроллера:
// would pull all recipes that have ingredients 1 and 2
$recipes = $this->Recipe->find('all', array(
'conditions' => array(
'id' => $this->Recipe->Ingredient->getRecipesFromIngredients(array(1,2))
)
));