php Doctrine setParameter 和 Invalid parameter number

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16017741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:22:50  来源:igfitidea点击:

Doctrine setParameter and Invalid parameter number

phpsymfonydoctrine-orm

提问by Florian Mithieux

After many tries, I think I finally know the documentation by heart. Then, I need your help .. I don't understand why Doctrine show me this error :

经过多次尝试,我想我终于把文档记住了。然后,我需要你的帮助..我不明白为什么 Doctrine 会显示这个错误:

Invalid parameter number: number of bound variables does not match number of tokens

参数编号无效:绑定变量的数量与令牌数量不匹配

Here is my code :

这是我的代码:

$qb = $this->em->createQueryBuilder();
$qb->select('m')
   ->from('Entities\Marque', 'm')
   ->leftJoin('m.magasin', 'ma')
   ->where('m.nom = :marque AND ma.nom LIKE :magasin')
   ->setParameter('marque', $marque)
   ->setParameter('magasin', '%'.$matchesNumber[1].'%');
$results = $qb->getQuery()->getArrayResult();

Thank you in advance for your answer.

预先感谢您的回答。

回答by Yes Barry

This also happens if you accidentally use more than one where(), which happened to me once.

如果您不小心使用了多个where(),也会发生这种情况,这发生在我身上一次。

$em    = $this->getEntityManager();
$query = $em->createQueryBuilder()
    ->from('AppBundle:SomeEntity', 's')
    ->select('s')
    ->where('s.foo = :foo')
    ->where('s.bar = :bar') // <- HERE
    ->setParameter('foo', 'Foo Value')
    ->setParameter('bar', 'Bar Value');

Should be:

应该:

$em    = $this->getEntityManager();
$query = $em->createQueryBuilder()
    ->from('AppBundle:SomeEntity', 's')
    ->select('s')
    ->where('s.foo = :foo')
    ->andWhere('s.bar = :bar') // <- CHANGE TO andWhere()
    ->setParameter('foo', 'Foo Value')
    ->setParameter('bar', 'Bar Value');

Hope this helps someone.

希望这可以帮助某人。

回答by DerStoffel

I presume ->setParameter overrides the previous one.

我认为 ->setParameter 会覆盖前一个。

For multiple Parameters use:

对于多个参数使用:

->setParameters(['key1' => $value1, 'key2' => $value2])

See Doctrine Upgrade:

见教义升级:

From now on, parameters in queries is an ArrayCollection instead of a simple array. This >affects heavily the usage of setParameters(), because it will not append anymore parameters >to query, but will actually override the already defined ones. Whenever you are retrieving a >parameter (ie. $query->getParameter(1))

从现在开始,查询中的参数是一个 ArrayCollection 而不是一个简单的数组。这 > 严重影响了 setParameters() 的使用,因为它不会追加更多参数 > 到查询,但实际上会覆盖已经定义的参数。每当您检索 > 参数时(即 $query->getParameter(1))

Doctrine Upgrade Description

教义升级说明

Maybe that also applies to setParameter?

也许这也适用于 setParameter?

回答by Florian Mithieux

I'm so sorry .. I just found my error .. Later, like more later in my code .. I type a new query with my old "$qb" .. I'm such a noob !

我很抱歉.. 我刚刚发现了我的错误.. 后来,在我的代码中更晚了.. 我用我的旧“$qb”输入了一个新查询..我真是个菜鸟!

回答by macchokri

$qb = $this->em->createQueryBuilder();
$parameters = array('marque'=>$marque, 'magasin'=>'%'.$matchesNumber[1].'%');
$qb->select('m')
   ->from('Entities\Marque', 'm')
   ->leftJoin('m.magasin', 'ma')
   ->where('m.nom = :marque')
   ->andWhere('ma.nom LIKE :magasin')
   ->setParameters($parameters);

$results = $qb->getQuery()->getArrayResult();