Symfony 3 - 从Repository返回奇怪的数据

时间:2017-10-16 09:29:28

标签: php symfony doctrine

我有Repository方法,它返回缺少发票价格的总和

public function getAmountToPay()
{
    $query = $this->createQueryBuilder('i')
        ->select('c.name, (i.price - COALESCE(sum(p.amount), 0)) as price')
        ->leftJoin('i.payments', 'p')
        ->join('i.company', 'c')
        ->where('i.dueDate > :now')
        ->groupBy('i.id')
        ->having('sum(i.price) > SUM(p.amount) OR SUM(p.amount) IS NULL ')
        ->setParameter('now', new \DateTime())
        ->getQuery();

    return $query->getResult();
}

Pure SQL返回正常的行但是如果我在doctrine中执行它,它会在数组中返回一些奇怪的数据,其中包含0个值:

"afterDeadline" => array:3 [▼
0 => array:2 [▼
  "name" => "Example company #1"
  "price" => "0"
]
1 => array:2 [▼
  "name" => "Example company #2"
  "price" => "0"
]
2 => array:2 [▼
  "name" => "Example company #1"
  "price" => "117.99000000000001"
]

为什么公司的价值为0?只有最后一个数组索引可以。

1 个答案:

答案 0 :(得分:1)

您是否尝试过madalinivascu建议的解决方案。它看起来像这样:

public function getAmountToPay()
{
    $query = $this->createQueryBuilder('i')
        ->select('c.name, (i.price - COALESCE(sum(p.amount), 0)) as price')
        ->leftJoin('i.payments', 'p')
        ->join('i.company', 'c')
        ->where('i.dueDate > :now')
        ->andWhere('i.price != 0')
        ->groupBy('i.id')
        ->having('sum(i.price) > SUM(p.amount) OR SUM(p.amount) IS NULL ')
        ->setParameter('now', new \DateTime())
        ->getQuery();

    return $query->getResult();
}