我有一组食谱,每个都包含类别。这是我的模型:
class Recipe extends \Model {
public static $_table = "recipe";
public function categories() {
return $this->has_many_through('Category');
}
}
class Category extends \Model {
public static $_table = "category";
public function categories() {
return $this->has_many_through('Recipe');
}
}
以及将两者联系起来的表格:
class CategoryRecipe extends \Model {
public static $_table = "category_recipe";
}
现在我需要创建一个查询来获取一个/多个类别下的所有食谱。实现这一目标的方法是什么?我想避免做这样的事情:
$results = $app['paris']->getModel('CategoryRecipe')
->where_in("category_id",$selected_categories)
->find_many();
foreach($results as $result) {
$recipe = $app['paris']->getModel('Recipe')
->where('id',$result->recipe_id)
->find_one();
var_dump($receta->name);
}
创建过滤器?模型内部的功能?是不是可以让它更优雅?