Draeli
Draeli

Reputation: 162

Doctrine ORM Pagination and use with Twig

I use Symfony2 with Doctrine ORM and Twig for the view part.

Actually I write a class to static call doctrine pagination like this :

use Doctrine\ORM\Tools\Pagination\Paginator;

class DoctrineHelp
{
    static public function paginate(Query $query, &$pageSize = 10, &$currentPage = 1){
        $pageSize = (int)$pageSize;
        $currentPage = (int)$currentPage;

        if( $pageSize < 1 ){
            $pageSize = 10;
        }

        if( $currentPage < 1 ){
            $currentPage = 1;
        }

        $paginator = new Paginator($query);

        $paginator
            ->getQuery()
            ->setFirstResult($pageSize * ($currentPage - 1))
            ->setMaxResults($pageSize)
        ;

        return $paginator;
    }
}

And in the controllers (simple Sf2 indexAction in controller) create request, call them and set all for the view part by :

$page = 1;
$pageSize = 5;
$articlesQuery = $em->getRepository('MyOwnBundle:Article')->getBlogArticles($languageId);
$ArticlesPaginator = DoctrineHelp::paginate($articlesQuery, $pageSize, $page);
return array('Articles' => $ArticlesPaginator);

the getBlogArticles() method :

public function getBlogArticles($languageId){
    $qb = $this->createQueryBuilder(self::ALIAS_NAME);

    $qb->andWhere(self::ALIAS_NAME . '.language = :languageId')->setParameter('languageId', $languageId);
    $qb->andWhere(self::ALIAS_NAME . ".disabled_at IS NULL");

    $qb->addOrderBy(self::ALIAS_NAME . '.published_at', 'DESC');
    $qb->addOrderBy(self::ALIAS_NAME . '.id', 'DESC');

    return $qb->getQuery();
}

Now when I display, I have unexpected result, the code for my try in Twig part :

{{ 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 #}

I signal the problem to Sf2 team : https://github.com/symfony/symfony/issues/11316 and Doctrine ORM team : http://www.doctrine-project.org/jira/browse/DDC-3207 but both team postulate there isn't problem.

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 ?

Thank you by advance.

Upvotes: 1

Views: 6483

Answers (1)

Draeli
Draeli

Reputation: 162

Finally the solution is to loop on iterator, that means :

{% for anArticle in Articles.iterator %}

In this case all dump inside add expected result :)

Upvotes: 2

Related Questions