我有以下代码,这给了我错误:
I have the following code which gives me the error:
Message: Invalid parameter number: number of bound variables does not match number of tokens
代码:
public function getCount($ids, $outcome)
{
if (!is_array($ids)) {
$ids = array($ids);
}
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->add('select', $qb->expr()->count('r.id'))
->add('from', 'MyEntityRating r');
if ($outcome === 'wins') {
$qb->add('where', $qb->expr()->in('r.winner', array('?1')));
}
if ($outcome === 'fails') {
$qb->add('where', $qb->expr()->in('r.loser', array('?1')));
}
$qb->setParameter(1, $ids);
$query = $qb->getQuery();
//die('q = ' . $qb);
return $query->getSingleScalarResult();
}
数据(或 $ids):
Data (or $ids):
Array
(
[0] => 566
[1] => 569
[2] => 571
)
DQL 结果:
q = SELECT COUNT(r.id) FROM MyEntityRating r WHERE r.winner IN('?1')
在研究这个问题时,我发现了一些对于遇到同样问题并寻求解决方案的人来说很重要的东西.
In researching this issue, I found something that will be important to anyone running into this same issue and looking for a solution.
从原来的帖子,下面这行代码:
From the original post, the following line of code:
$qb->add('where', $qb->expr()->in('r.winner', array('?1')));
将命名参数包装为数组会导致绑定参数编号问题.通过从它的数组包装中删除它:
Wrapping the named parameter as an array causes the bound parameter number issue. By removing it from its array wrapping:
$qb->add('where', $qb->expr()->in('r.winner', '?1'));
应该修复这个问题.这可能是 Doctrine 以前版本中的一个问题,但在 2.0 的最新版本中已修复.
This issue should be fixed. This might have been a problem in previous versions of Doctrine, but it is fixed in the most recent versions of 2.0.
这篇关于如何将 WHERE IN 与 Doctrine 2 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!