php - 无效参数编号 : number of bound variables does not match number of tokens in Doctrine

标签 php mysql doctrine

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

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

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

最佳答案

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

->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")

关于php - 无效参数编号 : number of bound variables does not match number of tokens in Doctrine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10170913/

相关文章:

php - 类中的匿名函数

php - 如何使用 JQuery AJAX 实现通用 CSRF token ?

python - pymysql:一次尝试,但无法捕获多sql错误

php - 如果 mysql_query() 失败,该怎么办?

php - 如何使用 SQL 获取 MySQL 停用词

php - 我如何只为我的循环下降这部分?

mysql - 从结果表中选择两个特定行?

php - 为什么一个 MySQL 数据库接受 DateTime 而另一个不接受?

postgresql - Doctrine 。关闭准备语句

php - Doctrine (symfony) YAML schema/fixture 约束问题