如何使用WHERE IN与Doctrine 2

编程入门 行业动态 更新时间:2024-10-22 08:28:23
本文介绍了如何使用WHERE IN与Doctrine 2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下代码给我错误:

消息:参数号无效:绑定变量的数量不标记的匹配数

代码:

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($ result ==='wins')$ qb-> add('where',$ qb-> expr() - > in('r.winner',array ))); if($ outcome ==='failed')$ qb-> add('where',$ qb-> expr() - > in('r.loser',array ))); $ 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')

解决方案

在研究这个问题时,我发现一些对任何人都很重要的事情进入同一个问题并寻找解决方案。

从原始帖子,以下代码行:

<$ ('r.winner',array('?1')));} $ {$ cb $> add('where',$ qb-> expr

将命名参数包装为数组会导致绑定的参数号问题。通过从数组包装中删除它:

$ qb-> add('where',$ qb-> expr ) - > in('r.winner','?1'));

此问题应该修复。这可能是以前版本的Doctrine中的问题,但它是在2.0的最新版本中修复的。

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')

解决方案

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'));

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

本文发布于:2023-10-09 06:13:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474921.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   Doctrine

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!