php Doctrine 和 Symfony2:WHERE a.title LIKE $array

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18634039/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:56:13  来源:igfitidea点击:

Doctrine and Symfony2: WHERE a.title LIKE $array

phpsqlsymfonydoctrine-ormdoctrine

提问by daneczech

Hey I'm writing this post because I have few problem passing an array value in the doctrine query.

嘿,我写这篇文章是因为我在学说查询中传递数组值几乎没有问题。

Here is the entire query as it is:

这是整个查询,因为它是:

$data = $request->request->all();
$dql   = "SELECT a FROM PfBlogBundle:Article a WHERE a.title LIKE '{$data['search']}' ORDER by a.id DESC";

If I print_r($data) I get the value so it's there somewhere. I just don't understand why it's not passing in the query.. Was expecting LIKE '{$data['search']}' to work but it doesn't.

如果我 print_r($data) 我得到值所以它在某处。我只是不明白为什么它没有在查询中传递.. 期待 LIKE '{$data['search']}' 工作,但它没有。

回答by Elias Van Ootegem

From what I can tell by your snippet, you're looking for something like this:

从你的片段中我可以看出,你正在寻找这样的东西:

$entityManager->getRepository('PfBlogBundle:Article')
              ->findBy(
                   array(
                      'key' => 'value'
                   )
               );

Where key is the property/field and the value is the value to look for. Check the Symfony manual page. The bit you're after is Fetching Objects from the Database.
To use a likein the where clause, refer to this SO question, on how to use setParameter. You'll get your query with this:

其中键是属性/字段,值是要查找的值。检查 Symfony 手册页。您所追求的是Fetching Objects from the Database
like在 where 子句中使用 a ,请参阅此 SO 问题,了解如何使用setParameter. 你会得到你的查询:

$repo = $entityManager->getRepository('PfBlogBundle:Article');
$query = $repo->createQueryBuilder('a')
               ->where('a.title LIKE :title')
               ->setParameter('title', '%'.$data['search'].'%')
               ->getQuery();

Of course, add wildcards to suite your needs. I've enclosed the $data['search']value in two %wildcards, which is slow, but then again: I don't know what you're actually doing. It might be that all you're after is the case-insensitive nature of LIKE, in which case the %can be left out all together...

当然,添加通配符以满足您的需求。我将$data['search']值括在两个%通配符中,这很慢,但话又说回来:我不知道你实际上在做什么。可能是您所追求的只是 的不区分大小写的性质LIKE,在这种情况下,%可以一起排除...

Based on your previous questions (BTW: consider accepting an answer once in a while):

基于您之前的问题(顺便说一句:考虑偶尔接受一个答案):

public function searchAction(Request $request)
{
    $data = $request->get->all();
    $repo = $this->getDoctrine()
                  ->getRepository('PfBlogBundle:Article');
    $query = $repo->createQueryBuilder('a')
                   ->where('a.title LIKE :title')
                   ->setParameter('title', '%'.$data['search'].'%')
                   ->getQuery();
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query->getResults(),//get the results here
        $this->requrest->get('page',1),
        4
    );
    return $this->render('PfBlogBundle:Default:blog.html.twig', array('pagination'=>$pagination));
}

But this is just a crude fix, Google doctrine-symfony pagination, there are many detailed blog-posts on the matter

但这只是一个粗略的修复,谷歌学说-symfony分页,有很多关于这个问题的详细博客文章

回答by daneczech

So I come back to close this topic. Here is the answer for the people who wants to try the same way as I did it. My answer here is based on Elias Van Ootegem tip, but I had to make few minor changes to that. So I write the controller solution:

所以我回来结束这个话题。这是想要像我一样尝试的人的答案。我在这里的回答是基于 Elias Van Ootegem 的提示,但我不得不对此做一些小改动。所以我写了控制器解决方案:

    public function searchAction(Request $request)
{
    $data = $request->get->all();
    $repo = $this->getDoctrine()
                  ->getRepository('PfBlogBundle:Article');
    $query = $repo->createQueryBuilder('a')
                   ->where('a.title LIKE :title')
                   ->setParameter('title', '%'.$data['search'].'%')
                   ->getQuery();
    $paginator  = $this->get('knp_paginator');
    $pagination = $paginator->paginate(
        $query->getResults(),//get the results here
        $this->request->get('page',1),
        4
    );
    return $this->render('PfBlogBundle:Default:blog.html.twig', array('pagination'=>$pagination));
}

Because the pagination is working with the get method, I had to change the method of the form to get as well... Than, this make me change this line in Elias code:

因为分页正在使用 get 方法,所以我不得不更改表单的方法来获取......比,这让我在 Elias 代码中更改了这一行:

$data = $request->request->all();

to

$data = $request->get->all();

Hope it will helpful for someone!

希望它会对某人有所帮助!