Symfony 对细枝进行分页和使用
我使用Symfony2和ORM以及Twig作为视图部分 实际上,我编写了一个静态调用分页类,如下所示:Symfony 对细枝进行分页和使用,symfony,orm,doctrine-orm,pagination,twig,Symfony,Orm,Doctrine Orm,Pagination,Twig,我使用Symfony2和ORM以及Twig作为视图部分 实际上,我编写了一个静态调用分页类,如下所示: use Doctrine\ORM\Tools\Pagination\Paginator; class DoctrineHelp { static public function paginate(Query $query, &$pageSize = 10, &$currentPage = 1){ $pageSize = (int)$pageSize;
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;
}
}
getBlogArticles()方法:
现在,当我显示时,我得到了意外的结果,我的try-in-Twig部分的代码:
{{ 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 #}
我向Sf2团队发出问题信号:
和ORM团队:
但两个团队都认为没有问题
现在我的问题是,如果{loop.last}和{loop.length}没有提供正确的结果,我该如何处理这个对象?
(实际上,我对{loop.first}}使用了一个技巧,不管当前页码是多少,都能提供正确的结果(至少对我来说,这是预期的结果))
如果两个团队都假设没有问题,怎么可能纠正这个问题
提前感谢。最后,解决方案是在迭代器上循环,这意味着:
{% for anArticle in Articles.iterator %}
在这种情况下,所有dump In add预期结果:)请将
getBlogArticles()
存储库方法添加到问题中。Paginator
是Doctrine\ORM\Tools\Pagination\Paginator
?
{{ 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 #}
{% for anArticle in Articles.iterator %}