Kohana 3 ORM:如何执行具有2个多对多关系的查询

问题描述:

我有一个产品模型,其中定义了2个多对多关系.

I have a products model with 2 many to many relationships defined.

protected $_has_many = array
(
'foodcats' => array('model' => 'foodcat',   'through' => 'products_foodcats'),
'foodgroups' => array('model' => 'foodgroup', 'through' => 'products_foodgroups')
)

我需要一个查询,以查找具有给定foodcat id和给定foodgroup名称的产品. 我知道我可以执行以下操作来获取具有给定foodcat ID的所有产品

I need a query where I find products with a given foodcat id and a given foodgroup name. I know I can do the following to get all products with a given foodcat id

$foodcat = ORM::factory('foodcat',$foodCatId);
$products = $foodcat->products->find_all();

但是,如何查询该食物猫中也属于食物组主菜"的产品?

But how do I query for products in that foodcat that also are in the foodgroup 'Entrees'?

谢谢!

,将给出未知的列错误.

in Kohana 3.1 without using DB::expr, will give unknown column error.

ORM::factory('product')
->join('foodcats','INNER')
->on('foodcats.id','=', DB::expr($foodcats_id))
->join('foodgroups','INNER')
->on('foodgroups.name','=', DB::expr($foodgroups_name))
->find_all();