使用 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/