Martin antonsson
Martin antonsson

Reputation: 121

Symfony function search wih query builder

I would like to create a function to search for a movie through the query builder

I have a table Movie:

     1. Id
     2. Titre
     3. Content

And i have class MovieRepository :

class MovieRepository extends EntityRepository
{
    public function myFindAll()
        {
        return $this->createQueryBuilder('a')
        ->getQuery()
        ->getResult();
        }


    public function getSearchMovies($movie){


            $qb = $this->createQueryBuilder('m')
                            ->where('m.title LIKE :title')
                                ->setParameter('title', '%' . $movie->getTitle() . '%')
                                    ->orderBy('m.title', 'DESC')
                                        ->getQuery();


    }   
}

Also i have MovieController :

public function indexAction()
        {
            $movie = new Movie;
            $form = $this->createForm(new SearchMovieType(), $movie);

            $request = $this->getRequest();
            if ($request->getMethod() == 'POST') {
                $form->bind($request);
                $movies = $this->getDoctrine()
                        ->getManager()
                            ->getRepository('AreaDownloadBundle:Movie')
                                ->getSearchUsers($movie);

                return $this->render('AreaDownloadBundle:Download:index.html.twig', array('form' => $form->createView(),array('movies' => $movies)));
            } else {

                $movies = $this->getDoctrine()
                            ->getManager()
                                ->getRepository('AreaDownloadBundle:Movie')
                                    ->myFindAll();

                return $this->render('AreaDownloadBundle:Download:index.html.twig',array('form' => $form->createView(), 'movies' => $movies));
            }
        }

SearchMovieType :

class SearchMovieType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder

            ->add('title','text', array('required' => false, ))
                ;
    }

And i have index.hml.twig, which can display movies with a search bar :

{% extends "::template.html.twig" %}
{% block body %}
<form action="{{ path('area_download_index') }}"  method="post">

    <div id="bar">          
        {{ form_widget(form.title) }}
        <input type="submit" value="Chercher"> 
        {{ form_rest(form) }}
    </div>
</form> 

    {% for movie in movies %}
       {{ movie.title }}
       {{ movie.content }} 
    {% endfor %}
{% endblock %}

when I seized a title of a movie he sends me this error

Variable "movies" does not exist in AreaDownloadBundle:Download:index.html.twig at line 12

Upvotes: 0

Views: 324

Answers (1)

Alvin Bunk
Alvin Bunk

Reputation: 7764

Instead of posting it as a comment, it should have been posted as an answer in the correct formatting; like so:

return $this->render(
    'AreaDownloadBundle:Download:index.html.twig',
    array(
        'form' => $form->createView(),
        'movies' => $movies
    )
);

This definitely should fix the problem!

Upvotes: 1

Related Questions