title: KnpPaginatorBundle sort on join query
body: <p>I have a problem with the sort of bundle in question when I pass a join query customized.
  My main purpose is to reduce the queries run in when a pagination of 20 results, it performs more than 30 queries.</p>
  
  <p>But with a predefined query with joins other various tables "normally" is reduced to 2 queries, of course, with a saving of performance that will not hurt!</p>
  
  <p>The problem arises with the KnpPaginatorBundle, where there seems to use my running the same query the 30 queries.</p>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>public function indexAction(Request $request)
  {
      $em = $this-&gt;getDoctrine()-&gt;getManager();
      $form = $this-&gt;createForm(new SoundFilterType());
      if (!is_null($response = $this-&gt;saveFilter($form, 'sound', 'sound'))) {
          return $response;
      }
  
      $qb = $em-&gt;getRepository('AcmeSoundBundle:Soundtrack')-&gt;findAllJoin();
      $paginator = $this-&gt;filter($form, $qb, 'sound');
  
  
      return array(
          'form' =&gt; $form-&gt;createView(),
          'paginator' =&gt; $paginator,
      );
  }
  
  /**
   * @param QueryBuilder $qb
   * @param string       $name
   */
  protected function addQueryBuilderSort(QueryBuilder $qb, $name)
  {
       $alias = current($qb-&gt;getDQLPart('from'))-&gt;getAlias();
       if (is_array($order = $this-&gt;getOrder($name))) {
           $qb-&gt;orderBy($alias . '.' . $order['field'], $order['type']);
       }
  }
  
  /**
   * Filter form
   *
   * @param  FormInterface  $form
   * @param  QueryBuilder   $qb
   * @return SlidingPagination
   */
  protected function filter(FormInterface $form, QueryBuilder $qb, $name)
  {
      if (!is_null($values = $this-&gt;getFilter($name))) {
         if ($form-&gt;bind($values)-&gt;isValid()) {
             $this-&gt;get('lexik_form_filter.query_builder_updater')-&gt;addFilterConditions($form, $qb);
         }
      }
  
      $count = $this-&gt;getDoctrine()-&gt;getManager()
                  -&gt;createQuery('SELECT COUNT(c) FROM AcmeSoundBundle:Sound c')
                  -&gt;getSingleScalarResult();
  
      // possible sorting
      $this-&gt;addQueryBuilderSort($qb, $name);
      return $this-&gt;get('knp_paginator')-&gt;paginate($qb-&gt;getQuery()-&gt;setHint('knp_paginator.count', $count), $this-&gt;getRequest()-&gt;query-&gt;get('page', 1), 20, array('distinct' =&gt; false));
  
  }
  </code></pre>
  
  <p><strong>Query</strong></p>
  
  <pre><code>public function findAllJoin()
  {        
       $qb = $this-&gt;createQueryBuilder('q');
       $query = $qb-&gt;select('u')
            -&gt;from('AcmeSoundBundle:Sound', 'u')
            -&gt;innerJoin('u.artists', 'a')
            -&gt;leftJoin('u.versions', 'v')
            -&gt;leftJoin('v.instruments', 'i')
            -&gt;addOrderBy('u.dataIns', 'ASC');
  }
  </code></pre>
  
  <p>Here is a gist <a href="https://gist.github.com/Lughino/6358896" rel="nofollow">https://gist.github.com/Lughino/6358896</a></p>
  
  <p>I do not understand why it does not use my query ..</p>
  
  <p>Any idea?</p>
  
  <h2><strong>EDIT:</strong></h2>
  
  <p>For this problem I found the error:</p>
  
  <pre><code>public function findAllJoin()
  {        
      $qb = $this-&gt;createQueryBuilder('q');
      $query = $qb-&gt;select('u, a, v, i')
        -&gt;from('AcmeSoundBundle:Sound', 'u')
        -&gt;innerJoin('u.artists', 'a')
        -&gt;leftJoin('u.versions', 'v')
        -&gt;leftJoin('v.instruments', 'i')
        -&gt;addOrderBy('u.dataIns', 'ASC');
  }
  </code></pre>
  
  <p>instead:</p>
  
  <pre><code>public function findAllJoin()
  {        
      $qb = $this-&gt;createQueryBuilder('q');
      $query = $qb-&gt;select('u')
        -&gt;from('AcmeSoundBundle:Sound', 'u')
        -&gt;innerJoin('u.artists', 'a')
        -&gt;leftJoin('u.versions', 'v')
        -&gt;leftJoin('v.instruments', 'i')
        -&gt;addOrderBy('u.dataIns', 'ASC');
  }
  </code></pre>
  
  <p>The problem now is that if I select a column to sort the I shows only one result, and even when I do the sort fails, in fact the results are not sorted at all.</p>
  
  <p>Is there something I'm missing?</p>
  
  <h2><strong>EDIT2</strong></h2>
  
  <p>Solved!
  If I set my two are instantiated from from and messed with for that!
  In this way:</p>
  
  <pre><code>public function findAllJoin()
  {        
     $qb = $this-&gt;createQueryBuilder('u');
     $query = $qb-&gt;select('u, a, v, i')
                 -&gt;innerJoin('u.artists', 'a')
                 -&gt;leftJoin('u.versions', 'v')
                 -&gt;leftJoin('v.instruments', 'i')
                 -&gt;addOrderBy('u.dataIns', 'ASC');
  }
  </code></pre>
  
  <p>The problem is solved!</p>