【问题标题】:Get filtered related data without excluding first table rows if there's no related data in second table如果第二个表中没有相关数据,则获取过滤后的相关数据而不排除第一个表行
【发布时间】:2013-03-21 13:47:30
【问题描述】:

有两个相关的表,Car 和 CouponException,我想获取一系列模型中的所有汽车,并获取与每辆汽车相关的 CouponExceptions,但棘手的事情来了.. 我只想获取那辆车的 CouponException给定一个优惠券ID。所以我现在正在尝试的是:

$versions = Doctrine_Query::create()
    ->from('Car c, c.CouponException ce')
    ->whereIn('c.model', $models)
    ->addWhere('ce.coupon_id = ?', $cid)
    ->fetchArray();

但它只返回给我带有优惠券异常的汽车,我想要的是获取模型列表中的所有汽车,如果有一辆具有给定优惠券 ID 的汽车,则获取该汽车的 CouponException...

【问题讨论】:

    标签: php doctrine doctrine-1.2


    【解决方案1】:

    我必须使用 LEFT JOIN 来获取所有结果并使用“with”关键字过滤第二个表。

        $versions   = Doctrine_Query::create()
                        ->select('c.*, ce.*')
                        ->from('Car c')
                        ->leftJoin('c.CouponException ce WITH ce.coupon_id = '.$cid)
                        ->whereIn('c.model', $models)
                        ->fetchArray();
    

    现在可以了 :))))

    【讨论】:

      猜你喜欢
      • 2023-04-05
      • 1970-01-01
      • 2016-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-14
      • 2015-06-17
      相关资源
      最近更新 更多