pagerfanta/doctrine-phpcr-odm-adapter
Pagerfanta adapter for Doctrine PHPCR-ODM, enabling paginated results from PHPCR document queries. Integrates Pagerfanta with PHPCR-ODM query builders/documents so you can build pagers and render page links efficiently.
Installation
composer require pagerfanta/doctrine-phpcr-odm-adapter
Ensure doctrine/phpcr-odm and pagerfanta/pagerfanta are also installed.
Basic Usage
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrinePHPCRAdapter;
$dm = $this->get('doctrine_phpcr.odm.document_manager');
$queryBuilder = $dm->createQueryBuilder('AppBundle:NodeType')
->where('...');
$adapter = new DoctrinePHPCRAdapter($queryBuilder);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(10);
// Render in Twig:
{% for node in pagerfanta %}
{{ node.title }}
{% endfor %}
First Use Case Paginate a list of PHPCR ODM documents (e.g., CMS pages, blog posts) in a Symfony controller or Twig template.
Reuse Existing Queries
Leverage existing QueryBuilder instances from PHPCR ODM to avoid redundant logic:
$qb = $dm->createQueryBuilder('AppBundle:Article')
->where('published = :published')
->setParameter('published', true);
$adapter = new DoctrinePHPCRAdapter($qb);
Dynamic Filtering
Use Pagerfanta’s setCurrentPage() and getCurrentPage() to sync with URL parameters:
$page = $request->query->getInt('page', 1);
$pagerfanta->setCurrentPage($page);
Twig Extension Create a custom Twig extension to centralize pagination logic:
class PagerfantaExtension extends \Twig_Extension {
public function getFunctions() {
return [
new \Twig_SimpleFunction('paginate_phpcr', [$this, 'paginate']),
];
}
public function paginate($qb, $perPage = 10) {
$adapter = new DoctrinePHPCRAdapter($qb);
return new Pagerfanta($adapter, $perPage);
}
}
Usage in Twig:
{% set paginated = paginate_phpcr(queryBuilder, 10) %}
{% for item in paginated %}
{{ item.content }}
{% endfor %}
URL Generation
Use Pagerfanta\View\Template\DefaultTemplate for consistent pagination links:
{{ paginated|pagerfanta(pagerfanta.twig.default) }}
Lazy Loading Avoid loading all documents upfront by using Pagerfanta’s lazy-loading features:
$pagerfanta->setMaxPerPage(20); // Load only 20 at a time
Caching
Cache QueryBuilder instances or results if queries are static:
$cacheKey = 'articles_paginated_' . $page;
$pagerfanta = $cache->get($cacheKey, function() use ($qb) {
return new Pagerfanta(new DoctrinePHPCRAdapter($qb), 10);
});
PHPCR-Specific Quirks
QueryBuilder paths are correct (e.g., /site/articles).AppBundle:Article).orderBy clauses for consistent pagination:
$qb->orderBy('node.path', 'ASC'); // Critical for stability
Adapter Limitations
COUNT Emulation: Unlike Doctrine ORM, PHPCR ODM may not support COUNT(*) in all backends (e.g., Jackrabbit). Use getNbResults() carefully:
$adapter->getNbResults(); // May trigger full collection load
$countQb = $dm->createQueryBuilder('AppBundle:Article')
->select('COUNT(node)')
->where('...');
$adapter->setNbResults((int) $countQb->getSingleScalarResult());
Symfony Dependency Injection
services:
app.pagerfanta.phpcr.adapter:
class: Pagerfanta\Adapter\DoctrinePHPCRAdapter
arguments:
- '@doctrine_phpcr.odm.document_manager'
Query Logging Enable PHPCR ODM query logging to verify generated queries:
$dm->getConfiguration()->setSQLLogging(true);
Adapter Validation Check if the adapter is properly initialized:
if (!$adapter->getNbResults()) {
throw new \RuntimeException('Adapter not properly configured');
}
Custom Templates Override Pagerfanta’s default Twig templates for custom styling:
{% extends 'PagerfantaTwigBundle:Pagination:default.html.twig' %}
{% block pagination_link %}
<a href="{{ path('app_article_list', { page: page }) }}">{{ page }}</a>
{% endblock %}
Event Listeners
Hook into Pagerfanta events (e.g., build.view) to modify pagination behavior:
$pagerfanta->addListener('build.view', function($event) {
$view = $event->getView();
$view->parameters['template'] = 'custom_template.html.twig';
});
Hybrid Pagination Combine with other Pagerfanta adapters (e.g., Doctrine ORM) for mixed-data pagination:
$adapter = new Pagerfanta\Adapter\UnionAdapter([
new DoctrinePHPCRAdapter($phpcrQb),
new DoctrineORMAdapter($ormQb),
]);
How can I help you explore Laravel packages today?