Kohana 3 ORM:如何执行具有2个多对多关系的查询 [英] Kohana 3 ORM: How to perform query with 2 many to many relationships
本文介绍了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();
这篇关于Kohana 3 ORM:如何执行具有2个多对多关系的查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文