Kohana 3 ORM:如何使用2个多对多关系执行查询

Kohana 3 ORM:如何使用2个多对多关系执行查询,kohana,kohana-3,Kohana,Kohana 3,我有一个定义了2个多对多关系的产品模型 protected $_has_many = array ( 'foodcats' => array('model' => 'foodcat', 'through' => 'products_foodcats'), 'foodgroups' => array('model' => 'foodgroup', 'through' => 'products_foodgroups') ) 我需要一个查询,在那里我可以找到具

我有一个定义了2个多对多关系的产品模型

protected $_has_many = array
(
'foodcats' => array('model' => 'foodcat',   'through' => 'products_foodcats'),
'foodgroups' => array('model' => 'foodgroup', 'through' => 'products_foodgroups')
)
我需要一个查询,在那里我可以找到具有给定FoodCatID和给定foodgroup名称的产品。 我知道我可以执行以下操作以获得具有给定foodcat id的所有产品

$foodcat = ORM::factory('foodcat',$foodCatId);
$products = $foodcat->products->find_all();
但是,我如何查询该foodcat中也在foodgroup“entres”中的产品呢


谢谢

简单;你没有。你需要的是内在的联结,比如

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

在Kohana 3.1中,如果不使用
DB::expr
,将给出未知列错误

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();