title: Filter a listing in Symfony2?
body: <p>Hello I tried Symfony and I'm a very novice.
  I am looking for an elegant way to filter listings.</p>
  
  <p>Let me explain:</p>
  
  <p>I have two entities: Link and Tag. They are in multiple relation.</p>
  
  <p>On my index view I created this form. I did a findAll() to get all my tags for the select:</p>
  
  <pre><code>&lt;form method="GET" action=""&gt;
      &lt;input class="btn btn-default" type="submit"/&gt;
      &lt;select name="tags[]" class="selectpicker" multiple="yes"&gt;
          {% for tag in tags %}
              &lt;option value="{{ tag.id }}"&gt; {{ tag.title }}&lt;/option&gt;
          {% endfor %}
      &lt;/select&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>This is the way I grab all link order by DESC:</p>
  
  <pre><code>$links = $em-&gt;getRepository('TestDefaultBundle:Link')-&gt;findBy(
      array(),
      array('id' =&gt; 'desc')
  );
  </code></pre>
  
  <p>How can I collect the selected tags (in the controller) and grab all links filter by these selected tags.</p>
  
  <p>Another question I know we can generate a form for an entity but what about this kind of form?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>This is my indexAction:</p>
  
  <pre><code>public function indexAction(Request $request)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $tags = $em-&gt;getRepository('LanCrmBundle:LinkTag')-&gt;findAll();
  
      // Create the filter form.
      $form = $this-&gt;createFormBuilder()
          -&gt;add('tags', 'entity', array(
              'class' =&gt; 'LanCrmBundle:LinkTag',
              'multiple' =&gt; true,
              'expanded' =&gt; false,
              'query_builder' =&gt; function (EntityRepository $er) {
                  return $er-&gt;createQueryBuilder('u')
                      -&gt;orderBy('u.title', 'ASC');
              }
          ))
          -&gt;add('OK', 'submit')
          -&gt;getForm()
      ;
  
      $form-&gt;handleRequest($request);
  
      if ($form-&gt;isValid()) {
          $data = $form-&gt;getData();
  
          // Get all links filtered by tags.
          // How to use the $data to filter my links?
          $links = $em-&gt;getRepository('LanCrmBundle:Link')-&gt;findBy(
              array(),
              array('id' =&gt; 'desc')
          );
      } else {
          // Get all links.
          $links = $em-&gt;getRepository('LanCrmBundle:Link')-&gt;findBy(
              array(),
              array('id' =&gt; 'desc')
          );
      }
  
      $paginator = $this-&gt;get('knp_paginator');
      $pagination = $paginator-&gt;paginate(
          $links,
          $this-&gt;get('request')-&gt;query-&gt;get('page', 1),
          4
      );
  
      return $this-&gt;render('LanCrmBundle:Link:index.html.twig', array(
          'pagination' =&gt; $pagination,
          'tags' =&gt; $tags,
          'form' =&gt; $form-&gt;createView()
      ));
  }
  </code></pre>
  
  <p>I've got this error :</p>
  
  <p>A "__toString()" method was not found on the objects of type "Lan\CrmBundle\Entity\LinkTag" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path.</p>
  
  <p>StringCastException: A "__toString()" method was not found on the objects of type "Lan\CrmBundle\Entity\LinkTag" passed to the choice field. To read a custom getter instead, set the option "property" to the desired property path.</p>