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

时间:2012-04-16 08:33:54

标签: php mysql doctrine

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

  

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

但是,据我所知,$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;
    }

1 个答案:

答案 0 :(得分:16)

请勿使用引号括起查询文本中的任何参数!

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

应该是

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