php - Doctrine setParameter 和无效参数号

标签 php symfony doctrine-orm

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

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

这是我的代码:

$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();

预先感谢您的回答。

最佳答案

如果您不小心使用了多个 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');

应该是:

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

希望这对某人有帮助。

关于php - Doctrine setParameter 和无效参数号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16017741/

相关文章:

php - 使用sql获取网站关注者

php - 如何将链接文本交换为链接,将YouTube链接交换为在YouTube中将代码嵌入YouTube

php - 如何在功能测试之前预热 Symfony 环境以检查执行时间?

mysql - Doctrine2 - 哪种方法更合适?

javascript - 如何从 PHP 运行 if 语句来运行 JS 函数

php - 如何在 PHP 中覆盖 register_argc_argv?

javascript - Prestashop 1.7 入门主题,theme.yml 中的资源不起作用

php - 无法将现有实体映射到 symfony2 中的嵌入形式

php - 使用 doctrine symfony 清除只有一个实体/存储库的结果缓存

symfony - 多对多,Doctrine的实体生成器和复数化