php 无效的参数编号:绑定变量的数量与 Doctrine 中的标记数量不匹配

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10170913/
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-24 21:33:43  来源:igfitidea点击:

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

phpmysqldoctrine

提问by Roel Veldhuizen

Using Doctrine 2 I want to get some users that are contacts of another user. The table usercontains the mapping between those users. The query in the function will return the following error:

使用 Doctrine 2 我想获得一些用户是另一个用户的联系人。该表user包含这些用户之间的映射。函数中的查询将返回以下错误:

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

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

However to my best understanding $stris set to "b" and $ownerIdis set to "2" and both are assigned by the setParametersfunction.

然而,我最好的理解$str是设置为“b”并$ownerId设置为“2”,两者都是由setParameters函数分配的。

 protected function getContactBySubstring($str, $ownerId) {
        echo $str;
        echo $ownerId;
        $em = $this->getDoctrine()->getEntityManager();
        $qb = $em->createQueryBuilder();
        $qb->add('select', 'u')
                ->add('from', '\Paston\VerBundle\Entity\User u, \Paston\VerBundle\Entity\Contact c')
                ->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")
                ->add('orderBy', 'u.firstname ASC, u.lastname ASC')
                ->setParameters(array (1=> $ownerId, 2=> '%'.$str.'%'));

        echo $qb->getDql();
        $query = $qb->getQuery();
        $users = $query->getResult();
        foreach($users as $user)
            echo $user->getUsername();
        exit;
        //return $contacts;
    }

回答by adavea

Don't surround any of the parameters in your query text with quotes!

不要用引号将查询文本中的任何参数括起来!

->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE '?2'")

should be

应该

->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE ?2")