错误:在Symfony3中使用带有Query-Builder的Count的左连接时出现无效的PathExpression(Doctrine2)

时间:2017-08-24 15:31:54

标签: symfony doctrine-orm query-builder

我有一个实体ClientFacture,每个Client都有许多Facture(:ManyToOne关系,使用由Facture->Clientid代表的外键, 我想列出所有Client以及他拥有的Facture的数量。

   ////////////////////////////////
   // id //  name   // factures  //
   ////////////////////////////////
   // 2  // someone // 16        //
   ////////////////////////////////

我想要实现的SQL逻辑将是这样的:

SELECT c.name as name , f.client_id, Count(*) as factures
FROM facture as f INNER JOIN client as c
ON f.client_id = c.id 
WHERE f.client_id != 0 AND c.state = 1
GROUP BY c.id

到目前为止,我使用查询构建器执行的操作是FactureRepository上的以下内容,该Facture仅识别public function findactivityList() { return $this->createQueryBuilder('f') ->select('f.clientId','count(f.clientId) as factures','c.name') ->leftJoin('servicomBundle:FComptet ','c','WITH' ,'f.clientId = c.id') ->where('f.clientId != 0') ->andWhere('c.state = 1') ->groupBy('f.clientId') ->getQuery() ->execute() ; } 实体。

[Semantical Error] line 0, col 9 near 'clientId, count(f.clientId)': Error: Invalid PathExpression. Must be a StateFieldPathExpression.

我收到此错误

{{1}}

1 个答案:

答案 0 :(得分:1)

尝试改变这个:

->select('f.clientId','count(f.clientId) as factures','c.name')

到此:

->select('IDENTITY(f.clientId)','count(f.clientId) as factures','c.name')

您可以使用IDENTITY函数在查询中选择外键ID