Doctrine 2 Querybuilder-两个联接导致错误的总和结果

时间:2019-06-09 19:43:25

标签: doctrine-orm

我需要生成报告,这项工作正常

    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb->select('
            c.category, 
            SUM(s.fulfillable)   AS fulfillable
        ')
        ->from('App\Entity\Catalog', 'c')
        ->leftJoin('App\Entity\Stock', 's', Expr\Join::WITH, 'c.sku = s.sku')
        ->groupBy('c.category')
    ;

但是当我也想对交易结果求和(多个)时,我应得的总和就不再成立了

    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb->select('
            c.category, 
            SUM(s.fulfillable) AS fulfillable,
            SUM(t.sold)        AS sold  
        ')
        ->from('App\Entity\Catalog', 'c')
        ->leftJoin('App\Entity\Stock', 's', Expr\Join::WITH, 'c.sku = s.sku')  // Only one Stock
        ->leftJoin('App\Entity\Transaction', 't', Expr\Join::WITH, 'c.sku = t.sku') // Multiple Transactions
        ->groupBy('c.category')
    ;

在教义ORM中是否有针对此类情况的解决方案?还是我必须对子选择使用原始sql查询。

谢谢你和最好的问候

0 个答案:

没有答案