How to use WHERE IN with Doctrine 2(如何将 WHERE IN 与 Doctrine 2 一起使用)
问题描述
我有以下代码,这给了我错误:
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 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何将 WHERE IN 与 Doctrine 2 一起使用


- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01