gpt4 book ai didi

symfony - 使用 Doctrine2 和 QueryBuilder 聚合 SUM

转载 作者:行者123 更新时间:2023-12-03 22:05:50 27 4
gpt4 key购买 nike

在我的存储库类中我有这个,但查询不起作用。

 public function getResultsByName($page, $resultsCount, array $request_arr){     
$qb = $this->createQueryBuilder('xx');
$qb->addSelect('SUM(xx.quantity) as total')
->leftJoin('xx.reception', 'x')
->addSelect('x')
->leftJoin('x.purchase', 'p')
->addSelect('p')
->leftJoin('p.provider', 'pr')
->addSelect('pr')
->where('pr.id = :company_id')
->setParameter('company_id', $request_arr['company_id']);

$query = $qb->getQuery();

return parent::getPaginator($query, $page, $resultsCount); }

错误出现在我的 Twig 模板中,这是其中的重要部分

    {% for result in results %}
<tr>
<td>{{result.reception.id}}</td>
<td>{{result.reception.date|date('d-m-Y')}}</td>
<td>{{result.reception.purchase.id}}</td>
<td>{{result.reception.purchase.provider.name|upper}} [{{result.reception.purchase.provider.id}}]</td>
<td>{{result.purchaseProduct.name |upper}} [{{result.purchaseProduct.productCode |upper}}]</td>
<td>{{result.purchasePrice}}</td>
<td>{{result.quantity}}</td>
<td></td>
<td>{{result.quantityStock}}</td>
</tr>
{% endfor %}

最佳答案

因为您在查询中有两个选择,所以您的结果对象是这样的数组:

array(
'total' => $total,
'xx' => array(
'reception' => $reception,
'quantityStock' => $quantityStock,
[...]
)
);

要访问您在 twig 中的属性,您必须像这样访问它:

{{result.xx.reception}}
{{result.total}}

关于symfony - 使用 Doctrine2 和 QueryBuilder 聚合 SUM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17265792/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com