Invalid parameter number: number of bound variable

2019-01-15 12:00发布

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

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

However to my best understanding $stris set to "b" and $ownerId is set to "2" and both are assigned by the setParameters function.

 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;
    }

1条回答
Deceive 欺骗
2楼-- · 2019-01-15 12:19

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")
查看更多
登录 后发表回答