Created
July 14, 2011 13:30
-
-
Save makasim/1082450 to your computer and use it in GitHub Desktop.
doctrin2 + pagination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Repository | |
namespace Rj\BlogBundle\Entity; | |
use Pagerfanta\Pagerfanta; | |
use Pagerfanta\Adapter\DoctrineORMAdapter; | |
/** | |
* | |
* ArticleRepository | |
*/ | |
class ArticleRepository extends AbstractRepository | |
{ | |
public function paginateApproved() | |
{ | |
$qb = $this->createQueryBuilder('a') | |
->where('a.approved = :approved') | |
->setParameter('approved', true) | |
->orderBy('a.creationTime', 'DESC'); | |
return new Pagerfanta(new DoctrineORMAdapter($qb->getQuery())); | |
} | |
} | |
// Controller | |
namespace Rj\BlogBundle\Controller; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | |
use Pagerfanta\View\DefaultView; | |
class ArticleController extends Controller | |
{ | |
/** | |
* | |
* @Template() | |
*/ | |
public function listAllAction($page) | |
{ | |
$pager = $this->get('repository.article')->paginateApproved(); | |
$pager->setMaxPerPage(2); | |
$pager->setCurrentPage($page); | |
$articles = $pager->getCurrentPageResults(); | |
$controller = $this; | |
$pagerWidgetRenderer = new DefaultView(); | |
$pagerWidget = $pagerWidgetRenderer->render($pager, function($page) use($controller) { | |
return $controller->generateUrl('rj_blog_article_public_list', array('page' => $page)); | |
}); | |
return array('articles' => $articles, 'pagerWidget' => $pagerWidget); | |
} | |
// View | |
{% extends 'CoreBundle:Layout:default.html.twig' %} | |
{% block head_stylesheets %} | |
{{ parent() }} | |
{% stylesheets | |
'@BlogBundle/Resources/public/css/pagerfanta.css' | |
%} | |
<link href="{{ asset_url }}" type="text/css" rel="stylesheet" /> | |
{% endstylesheets %} | |
{% endblock %} | |
{% block content %} | |
<table> | |
<tr> | |
<th>Title</th> | |
<th>Author Name</th> | |
<th>Creation Date</th> | |
</tr> | |
{% for article in articles %} | |
<tr> | |
<td><a href="{{ path('rj_blog_article_show', { 'articleId': article.id }) }}">{{ article.title }}</a></td> | |
<td>{{ article.authorName }}</td> | |
<td>{{ article.creationDate }}</td> | |
</tr> | |
{% else %} | |
<li>No article found</li> | |
{% endfor %} | |
<tr> | |
<td colspan="3">{{ pagerWidget|raw }}</td> | |
</tr> | |
</table> | |
{% endblock %} | |
// Routing | |
rj_blog_article_public_list: | |
pattern: /bblog/{page} | |
defaults: | |
_controller: BlogBundle:Article:listAll | |
page: 1 | |
requirements: | |
_method: "get" | |
page: \d+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment