title: Doctrine ORM Pagination and use with Twig
body: <p>I use Symfony2 with Doctrine ORM and Twig for the view part.</p>
  
  <p>Actually I write a class to static call doctrine pagination like this :</p>
  
  <pre><code>use Doctrine\ORM\Tools\Pagination\Paginator;
  
  class DoctrineHelp
  {
      static public function paginate(Query $query, &amp;$pageSize = 10, &amp;$currentPage = 1){
          $pageSize = (int)$pageSize;
          $currentPage = (int)$currentPage;
  
          if( $pageSize &lt; 1 ){
              $pageSize = 10;
          }
  
          if( $currentPage &lt; 1 ){
              $currentPage = 1;
          }
  
          $paginator = new Paginator($query);
  
          $paginator
              -&gt;getQuery()
              -&gt;setFirstResult($pageSize * ($currentPage - 1))
              -&gt;setMaxResults($pageSize)
          ;
  
          return $paginator;
      }
  }
  </code></pre>
  
  <p>And in the controllers (simple Sf2 indexAction in controller) create request, call them and set all for the view part by :</p>
  
  <pre><code>$page = 1;
  $pageSize = 5;
  $articlesQuery = $em-&gt;getRepository('MyOwnBundle:Article')-&gt;getBlogArticles($languageId);
  $ArticlesPaginator = DoctrineHelp::paginate($articlesQuery, $pageSize, $page);
  return array('Articles' =&gt; $ArticlesPaginator);
  </code></pre>
  
  <p>the getBlogArticles() method :</p>
  
  <pre><code>public function getBlogArticles($languageId){
      $qb = $this-&gt;createQueryBuilder(self::ALIAS_NAME);
  
      $qb-&gt;andWhere(self::ALIAS_NAME . '.language = :languageId')-&gt;setParameter('languageId', $languageId);
      $qb-&gt;andWhere(self::ALIAS_NAME . ".disabled_at IS NULL");
  
      $qb-&gt;addOrderBy(self::ALIAS_NAME . '.published_at', 'DESC');
      $qb-&gt;addOrderBy(self::ALIAS_NAME . '.id', 'DESC');
  
      return $qb-&gt;getQuery();
  }
  </code></pre>
  
  <p>Now when I display, I have unexpected result, the code for my try in Twig part :</p>
  
  <pre><code>{{ dump(Articles|length) }} {# here "int 23" for example in this case because result without limit is 23, ok I understand #}
  {% for anArticle in Articles %}
  {{ dump(loop.first) }} {# as expected here "true" for the first and false for other #}
  {{ dump(loop.last) }} {# always "false" even for the fifth result who is suppose to be "true" #}
  {{ dump(loop.length) }} {# here return always "int 23" in spite expected 5 #}
  {% endfor %}
  {# for display my 5 object in spite loop.last and loot.length not expected result #}
  </code></pre>
  
  <p>I signal the problem to Sf2 team : <a href="https://github.com/symfony/symfony/issues/11316" rel="nofollow">https://github.com/symfony/symfony/issues/11316</a>
  and Doctrine ORM team : <a href="http://www.doctrine-project.org/jira/browse/DDC-3207" rel="nofollow">http://www.doctrine-project.org/jira/browse/DDC-3207</a>
  but both team postulate there isn't problem.</p>
  
  <p>Now my question is, how I'm suppose to work with the object if {{ loop.last }} and {{ loop.length }} doesn't provide correct result ?
  (Actually I use a trick with {{ loop.first }} who provide correct result in spite the current page number (this is for me the expected result at least))
  How it's possible to correct this if both team postulate there isn't problem ?</p>
  
  <p>Thank you by advance.</p>