Doctrine内部查询设置参数

时间:2017-03-27 09:15:09

标签: symfony doctrine-orm

我正在尝试执行内部查询并设置参数但没有成功,我总是得到:

  

参数号无效:绑定变量数与令牌数不匹配

这是我的疑问:

SELECT i2
FROM MyBundle:Identity i2
INNER JOIN MyBundle:Property\\Mapped\\Email propertyEmail WITH propertyEmail.identity = i2
INNER JOIN MyBundle:Channel channel WITH propertyEmail.channel = channel
WHERE ( 
    SELECT COUNT(mappedEmail)
    FROM MyBundle:Property\\Mapped\\Email mappedEmail
    WHERE mappedEmail.identity = i2  
    AND mappedEmail.channel = :channel 
    AND mappedEmail.createdAt <= :dateFrom
 ) > 0  
 AND IDENTITY(propertyEmail.channel) <> :channel
 AND propertyEmail.createdAt <= :dateFrom
 GROUP BY i2

这就是我作为子查询的方式:

$innerQuery = $queryBuilder->getEntityManager()->createQuery($query)
    ->setParameters([
        'channel' => $channelId,                             
        'dateFrom' => $dateFrom
    ])
;
$queryBuilder->andWhere($queryBuilder->expr()->notIn($alias, $innerQuery->getDQL()));

返回错误:

  

参数号无效:绑定变量数与令牌数不匹配

如果我将查询作为单个查询执行,则一切正常 谢谢你的建议。

1 个答案:

答案 0 :(得分:0)

尝试将参数应用于主查询,如下所示:

$innerQuery = $queryBuilder->getEntityManager()->createQuery($query);

$queryBuilder
 ->andWhere($queryBuilder->expr()->notIn($alias, $innerQuery->getDQL()))
  ->setParameters([
        'channel' => $channelId,                             
        'dateFrom' => $dateFrom
    ]);