'无效的参数编号:绑定变量的数量不匹配令牌的数量'Symfony
问题描述:
我正在研究一个symfony项目实体与查询生成器。当我尝试运行这个功能时,我遇到了这个问题。'无效的参数编号:绑定变量的数量不匹配令牌的数量'Symfony
非法参数编号:绑定变量的数量不匹配的令牌数量
public function json_filterAllproductsAction() {
$search = "";
$category = 1;
//Combine tables and create the query with querybuilder
$em = $this->container->get('doctrine.orm.entity_manager');
$qb = $em->createQueryBuilder();
$qb->select('p')
->from('EagleAdminBundle:Products', 'p')
->orderBy('p.id', 'DESC');
if ($category != 0) {
$qb->where($qb->expr()->in('p.category', '?1'))
->setParameter(1, $category);
}
$qb->where('p.productTitle LIKE :title')
->setParameter('title', "$search%");
//convert to json using "JMSSerializerBundle"
$serializer = $this->container->get('serializer');
$jsonproducts = $serializer->serialize($qb->getQuery()->getResult(), 'json');
return new Response($jsonproducts);
}
我认为错误是
$ QB->其中($ QB-> expr() - > in('p.category','?1')) - > setParameter(1,$ category);
这将是很大的帮助,有人可以帮助我。
答
这里有两个问题。首先是你最后的where子句覆盖第一个子句。这可以通过使用andWhere来解决。第二个是你的混合命名参数(:标题)与位置参数(?1)。混合是一个不错的选择。你并不需要expr对象。尝试:
$qb->select('product')
->from('EagleAdminBundle:Products', 'product')
->orderBy('product.id', 'DESC');
if ($category) {
$qb->andWhere('product.category IN (:category)');
$qb->setParameter('category', $category);
}
$qb->andWhere('product.productTitle LIKE :title');
$qb->setParameter('title', "$search%");
+0
非常感谢,它工作得很好 – vimuth
的可能的复制[无效参数编号:绑定变量的数量不匹配的令牌数量](http://stackoverflow.com/questions/2154119/invalid-parameter-number-number-of- bound-variables-does-not-match-number-of-tok) – Matheno