如何在 Symfony2 中进行 LIKE 数据库查询
这应该很简单,但我找不到有效的例子。这是一个抛出错误“参数编号无效:绑定变量的数量与标记数量不匹配”的控制器方法。我已成功发布“searchterm”变量,但无法使查询正常工作。缺什么?谢谢!
public function searchAction()
{
$request = $this->getRequest();
$searchterm = $request->get('searchterm');
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery("SELECT n FROM AcmeNodeBundle:Node n WHERE n.title LIKE '% :searchterm %'")
->setParameter('searchterm', $searchterm);
$entities = $query->getResult();
return array('entities' => $entities);
}
This should be simple but I can't find a working example. Here's a controller method that throws the error "Invalid parameter number: number of bound variables does not match number of tokens". I'm posting the "searchterm" variable successfully but can't get the query to work. What is missing? Thanks!
public function searchAction()
{
$request = $this->getRequest();
$searchterm = $request->get('searchterm');
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery("SELECT n FROM AcmeNodeBundle:Node n WHERE n.title LIKE '% :searchterm %'")
->setParameter('searchterm', $searchterm);
$entities = $query->getResult();
return array('entities' => $entities);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我的 Symfony2 项目的工作示例:
Working example from my Symfony2 project:
您应该转储创建的查询以便于调试。
我只能建议您也尝试一下查询构建器:
文档
You should dump the created query for easier debugging.
I can only suggest you also try the querybuilder:
doc
我认为这个选项也有帮助:
I think this option also helps:
WHERE n.title LIKE '% :searchterm %'
应该是
WHERE n.title LIKE :searchterm
WHERE n.title LIKE '% :searchterm %'
should be
WHERE n.title LIKE :searchterm
也许是AcmeNodeBundle\Node?在 DQL 中
AcmeNodeBundle:Node
:Node
- 命名参数maybe
AcmeNodeBundle\Node
? In DQLAcmeNodeBundle:Node
:Node
- named parameter