Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Doctrine Mongodb Odm Adapter Laravel Package

pagerfanta/doctrine-mongodb-odm-adapter

Doctrine MongoDB ODM adapter for Pagerfanta. Adds pagination support for ODM query builders and queries, returning paginated results with limits, offsets, and total counts—ideal for building pageable lists in Laravel, Symfony, or custom PHP apps.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require pagerfanta/doctrine-mongodb-odm-adapter
    

    Ensure doctrine/mongodb-odm and pagerfanta/pagerfanta are also installed.

  2. Basic Usage

    use Pagerfanta\Pagerfanta;
    use Pagerfanta\Adapter\DoctrineODMAdapter;
    use Doctrine\ODM\MongoDB\DocumentManager;
    
    $dm = app(DocumentManager::class);
    $adapter = new DoctrineODMAdapter($dm->getRepository('YourDocument'));
    $pagerfanta = new Pagerfanta($adapter);
    $pagerfanta->setMaxPerPage(10); // Default page size
    
  3. First Use Case: Paginating Queries

    $query = $dm->createQueryBuilder('YourDocument')->find();
    $adapter = new DoctrineODMAdapter($query);
    $pagerfanta = new Pagerfanta($adapter);
    $pagerfanta->setCurrentPage(1); // Fetch first page
    

Implementation Patterns

Query-Based Pagination

  • Dynamic Query Handling Use DoctrineODMAdapter with QueryBuilder for flexible pagination:

    $query = $dm->createQueryBuilder('User')
        ->field('status')->equals('active')
        ->sort('createdAt', 'DESC');
    $adapter = new DoctrineODMAdapter($query);
    
  • Repository Integration Leverage repository methods for cleaner code:

    $adapter = new DoctrineODMAdapter($dm->getRepository('User')->findAll());
    

View Integration

  • Twig Blade Rendering Use Pagerfanta’s built-in view helpers:

    $view = $pagerfanta->getIterator();
    // Pass `$view` to your template for iteration.
    
  • Custom View Templates Extend Pagerfanta’s SlidingView or CursorView for MongoDB-specific UX (e.g., scroll-based pagination).

Performance Optimization

  • Cursor-Based Pagination For large datasets, use cursor-based pagination:

    $query = $dm->createQueryBuilder('User')->sort('id', 'ASC');
    $adapter = new DoctrineODMAdapter($query, 10, 0, ['cursor' => true]);
    
  • Projection for Efficiency Limit fetched fields to reduce payload:

    $query->fields()->include('id')->include('name');
    

Gotchas and Tips

Common Pitfalls

  • Cursor vs. Offset Limiting MongoDB’s skip() (offset) is inefficient for large datasets. Prefer cursor-based pagination or limit() + sort().

    // Avoid:
    $query->skip(100)->limit(10); // Bad for large offsets
    // Use:
    $query->sort('id', 'ASC')->limit(10); // Better for cursor pagination
    
  • Hydration Overhead Pagerfanta loads all documents into memory for counting. For large datasets:

    • Use setUseOutputFromHydrator(true) to reduce memory.
    • Implement custom counting logic (e.g., countDocuments()).

Debugging

  • Adapter Validation Ensure the adapter receives a valid QueryBuilder or Repository:

    if (!$adapter->getQueryBuilder()) {
        throw new \RuntimeException('Invalid QueryBuilder provided.');
    }
    
  • MongoDB-Specific Errors Handle MongoDB\Driver\Exception\Exception for connection/query issues:

    try {
        $pagerfanta->getCurrentPageResults();
    } catch (\MongoDB\Driver\Exception\Exception $e) {
        \Log::error('MongoDB pagination error: ' . $e->getMessage());
    }
    

Extension Points

  • Custom Counting Override getNbResults() for complex aggregations:

    class CustomAdapter extends DoctrineODMAdapter {
        public function getNbResults() {
            return $this->queryBuilder->getQuery()->count();
        }
    }
    
  • Integration with API Platform Use Pagerfanta’s ApiExtension for automatic pagination in API responses:

    # config/packages/api_platform.yaml
    api_platform:
        pagination:
            enabled: true
            client_items_per_page: true
    
  • Caching Results Cache paginated results with Pagerfanta\Cache\Adapter:

    use Pagerfanta\Cache\DoctrineCacheAdapter;
    $cache = new DoctrineCacheAdapter($dm->getCache());
    $pagerfanta->setCache($cache);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui