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 一起使用


基础教程推荐
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 如何替换eregi() 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01