【问题标题】:Kohana 3 ORM: How to perform query with 2 many to many relationshipsKohana 3 ORM:如何使用 2 个多对多关系执行查询
【发布时间】:2011-03-18 05:22:37
【问题描述】:

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

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

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

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

但是,我如何查询该 foodcat 中也位于 foodgroup 'Entrees' 中的产品?

谢谢!

【问题讨论】:

    标签: kohana kohana-3


    【解决方案1】:

    简单;你没有。你需要的是INNER JOIN,比如;

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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-04-01
        • 1970-01-01
        • 1970-01-01
        • 2012-02-25
        • 1970-01-01
        • 1970-01-01
        • 2017-06-09
        • 1970-01-01
        相关资源
        最近更新 更多