Kohana 3 ORM: how to fulfill a request with 2 many relationships

I have a product model with 2 many many relationships.

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

I need a request where I find products with the specified foodcat identifier and the name of this food group. 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 can I request products in this food package that are also in the "Entrees" food group?

Thanks!

+1
source share
2 answers

Simply; you do not do that. What you need is an INNER JOIN, for example:

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

in Kohana 3.1 without using DB::expr will give an 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(); 
0
source

All Articles