views:

106

answers:

1

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')
)

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'?

Thanks!

A: 

Simply; you don't. What you need is INNER JOIN, like;

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

related questions