Doctrine and Symfony2: WHERE a.title LIKE $ array

Hey, I am writing this post because I have few problems passing the value of an array in a doctrine request.

Here is the whole query:

$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 that it is somewhere. I just don't understand why this is not being passed in the request. I was expecting LIKE '{$ data [' search ']}' to work, but it is not.

+4
source share
2 answers

From what I can tell from your snippet, you are looking for something like this:

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

Where the key is the / field property and the value is the value to look for. Check out the symfony man page. The bit you are using is Extracting objects from the database .
To use like in the where clause, refer to this SO question on how to use setParameter . You will receive a request:

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

Of course, add wildcards to suit your needs. I put the value of $data['search'] in two % wildcards, which is slow, but again: I donโ€™t know what you are actually doing. Perhaps all you need is case insensitivity like , in which case % can be left unchanged ...

According to your previous questions (BTW: think about accepting an answer from time to time):

 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 rough fix, Google doctrine-symfony pagination , there are many detailed blog posts on this subject

+17
source

So, I'm coming back to close this topic. Here is the answer for people who want to try the same way I do. My answer here is based on a tip from Elias Van Ootegem, but I had to make minor changes to it. Therefore, I am writing a controller solution:

  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)); } 

Since pagination works with the get method, I had to change the form method in order to get as well ... What made me change this line in the Elias code:

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

to

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

Hope this helps someone!

0
source

Source: https://habr.com/ru/post/1500703/


All Articles