参数号无效:绑定变量的数量与Doctrine中的令牌数不匹配 [英] Invalid parameter number: number of bound variables does not match number of tokens in Doctrine
本文介绍了参数号无效:绑定变量的数量与Doctrine中的令牌数不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用Doctrine 2我想让一些与其他用户的联系的用户。表 user
包含这些用户之间的映射。函数中的查询将返回以下错误:
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:
参数号无效:绑定变量的数量与令牌数不匹配。 p>
Invalid parameter number: number of bound variables does not match number of tokens.
然而,为了最好的理解, $ str
设置为b和 $ ownerId
设置为2,都由 setParameters
函数分配。
However to my best understanding $str
is 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;
}
推荐答案
您的查询文本中带有引号的参数!
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'")
应该是
->add('where', "c.owner = ?1 AND c.contact = u.id AND u.username LIKE ?2")
这篇关于参数号无效:绑定变量的数量与Doctrine中的令牌数不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文