本文介绍了如何使用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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 06:43