使用Doctrine 2,我想让一些用户成为另一个用户的联系人。该表user包含这些用户之间的映射。函数中的查询将返回以下错误:
user
参数编号无效:绑定变量的数量与令牌的数量不匹配。
但是,据我所知,$str它设置为“ b”,$ownerId设置为“ 2”,两者均由该setParameters功能分配。
$str
$ownerId
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")