使用Doctrine2和QueryBuilder聚合SUM

时间:2013-06-23 22:08:08

标签: symfony doctrine sum query-builder symfony-2.2

在我的存储库类中,我有这个,但查询无效。

 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); }

错误出现在我的树枝模板中,这是它的重要部分

    {% 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 %}

1 个答案:

答案 0 :(得分:3)

由于您在查询中有两个选择,因此您的结果对象是此类数组:

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

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

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