如何在 Symfony2 中进行 LIKE 数据库查询

发布于 2024-12-03 21:45:08 字数 572 浏览 1 评论 0原文

这应该很简单,但我找不到有效的例子。这是一个抛出错误“参数编号无效:绑定变量的数量与标记数量不匹配”的控制器方法。我已成功发布“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

秉烛思 2024-12-10 21:45:08

我的 Symfony2 项目的工作示例:

$qb = $this->createQueryBuilder('u');
$qb->where(
         $qb->expr()->like('u.username', ':user')
     )
     ->setParameter('user','%Andre%')
     ->getQuery()
     ->getResult();

Working example from my Symfony2 project:

$qb = $this->createQueryBuilder('u');
$qb->where(
         $qb->expr()->like('u.username', ':user')
     )
     ->setParameter('user','%Andre%')
     ->getQuery()
     ->getResult();
蛮可爱 2024-12-10 21:45:08

您应该转储创建的查询以便于调试。

我只能建议您也尝试一下查询构建器:

$qb = $em->createQueryBuilder();
$result = $qb->select('n')->from('Acme\NodeBundle\Entity\Node', 'n')
  ->where( $qb->expr()->like('n.title', $qb->expr()->literal('%' . $searchterm . '%')) )
  ->getQuery()
  ->getResult();

文档

You should dump the created query for easier debugging.

I can only suggest you also try the querybuilder:

$qb = $em->createQueryBuilder();
$result = $qb->select('n')->from('Acme\NodeBundle\Entity\Node', 'n')
  ->where( $qb->expr()->like('n.title', $qb->expr()->literal('%' . $searchterm . '%')) )
  ->getQuery()
  ->getResult();

doc

毁梦 2024-12-10 21:45:08

我认为这个选项也有帮助:

$qb = $this->createQueryBuilder('u');
$qb->where('u.username like :user')
     ->setParameter('user','%hereIsYourName%')
     ->getQuery()
     ->getResult();

I think this option also helps:

$qb = $this->createQueryBuilder('u');
$qb->where('u.username like :user')
     ->setParameter('user','%hereIsYourName%')
     ->getQuery()
     ->getResult();
我的鱼塘能养鲲 2024-12-10 21:45:08

WHERE n.title LIKE '% :searchterm %'

应该是

WHERE n.title 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);

}

WHERE n.title LIKE '% :searchterm %'

should be

WHERE n.title 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);

}
时光与爱终年不遇 2024-12-10 21:45:08

也许是AcmeNodeBundle\Node?在 DQL 中 AcmeNodeBundle:Node :Node - 命名参数

maybe AcmeNodeBundle\Node? In DQL AcmeNodeBundle:Node :Node - named parameter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文