问题描述
我有以下代码,这给了我错误:
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 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)