如何将 WHERE IN 与 Doctrine 2 一起使用
我有以下代码,但出现错误:
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', '\My\Entity\Rating 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):
Array
(
[0] => 566
[1] => 569
[2] => 571
)
DQL 结果:
q = SELECT COUNT(r.id) FROM \My\Entity\Rating r WHERE r.winner IN('?1')
I have the following code which gives me the error:
Message: Invalid parameter number: number of bound variables does not match number of tokens
Code:
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', '\My\Entity\Rating 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();
}
Data (or $ids):
Array
(
[0] => 566
[1] => 569
[2] => 571
)
DQL result:
q = SELECT COUNT(r.id) FROM \My\Entity\Rating r WHERE r.winner IN('?1')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
最简单的方法是将数组本身绑定为参数:
The easiest way to do that is by binding the array itself as a parameter:
在研究这个问题时,我发现了一些对于遇到同一问题并寻求解决方案的人来说很重要的东西。
在原始帖子中,以下代码行:
将命名参数包装为数组会导致绑定参数编号问题。通过将其从数组包装中删除:
此问题应该得到解决。这可能是以前版本的 Doctrine 中的一个问题,但在最新版本的 2.0 中已修复。
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:
Wrapping the named parameter as an array causes the bound parameter number issue. By removing it from its array wrapping:
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.
并完成字符串解决方案
and for completion the string solution
我发现,尽管文档表明了这一点,但让它工作的唯一方法是这样的:
http://groups.google.com/group/doctrine-dev/browse_thread/thread/fbf70837293676fb
I found that, despite what the docs indicate, the only way to get this to work is like this:
http://groups.google.com/group/doctrine-dev/browse_thread/thread/fbf70837293676fb
我知道这是一篇旧帖子,但可能对某人有帮助。我会投票&通过解决有关 int 的评论中提出的问题来增强@Daniel Espendiller 的答案
为了以正确的方式使 int 工作,请确保数组中的值是 int 类型,您可以在传递之前键入强制转换为 int ...
测试了 select/在 symfony 3.4 & 中删除学说包:1.8
I know it's an old post but may be helpful for someone. I would vote & enhance @Daniel Espendiller answer by addressing the question asked in comments about ints
To make this work for int's in proper way, make sure the values in array are of type int, you can type cast to int before passing...
Tested for select/delete in symfony 3.4 & doctrine-bundle: 1.8
我知道 OP 的示例是使用 DQL 和查询生成器,但我在寻找如何从控制器或存储库类外部执行此操作时偶然发现了这一点,所以也许这会对其他人有所帮助。
您还可以通过以下方式从控制器执行
WHERE IN
:I know the OP's example is using DQL and the query builder, but I stumbled upon this looking for how to do it from a controller or outside of the repository class, so maybe this will help others.
You can also do a
WHERE IN
from the controller this way:这就是我的使用方式:
This is how I used it:
执行此操作的最佳方法 - 特别是如果您添加多个条件 - 是:
如果您的值数组包含字符串,则不能将 setParameter 方法与内爆字符串一起使用,因为您的引号将被转义!
The best way doing this - especially if you're adding more than one condition - is:
If your array of values contains strings, you can't use the setParameter method with an imploded string, because your quotes will be escaped!
在2016年发现了如何做到这一点:
https://redbeardtechnologies.wordpress.com/2011 /07/01/doctrine-2-dql-in-statement/
引用:
以下是正确的做法:
setParameters
方法将采用给定的数组并将其正确地内爆为用在“IN”语句中。Found how to do it in the year of 2016:
https://redbeardtechnologies.wordpress.com/2011/07/01/doctrine-2-dql-in-statement/
Quote:
Here is how to do it properly:
The method
setParameters
will take the given array and implode it properly to be used in the “IN” statement.还适用于:
Also works with:
我更喜欢:
I prefer:
我在同样的场景中遇到了困难,我必须对一组值进行查询。
以下内容对我有用:
http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/dql-doctrine-query-language.html#where-clause
数组数据示例(使用字符串和整数):
查询示例(适应您需要的地方):
I struggled with this same scenario where I had to do a query against an array of values.
The following worked for me:
http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/dql-doctrine-query-language.html#where-clause
Array data example (worked with strings and integers):
Query example (Adapt to where you need it):
这是几年后的事了,在一个遗留网站上工作......我一生都无法得到
->andWhere()
或->expr()->; in()
解决方案有效。最后查看了 Doctrine mongodb-odb 存储库,发现了一些非常有启发性的测试:
它对我有用!
您可以在 github 上找到测试 此处。对于澄清各种废话很有用。
注意:据我所知,我的设置使用的是 Doctrine MongoDb ODM v1.0.dev。
This is years later, working on a legacy site... For the life of me I couldn't get the
->andWhere()
or->expr()->in()
solutions working.Finally looked in the Doctrine mongodb-odb repo and found some very revealing tests:
It worked for me!
You can find the tests on github here. Useful for clarifying all sorts of nonsense.
Note: My setup is using Doctrine MongoDb ODM v1.0.dev as far as i can make out.