Doctrine : Pagination with left Joins

后端 未结 4 2105
长情又很酷
长情又很酷 2020-12-14 03:15

I would like to paginate a complex request with at least 2 left joins, but the pagination bundle I\'m using (KnpPaginationBundle) can\'t tell Doctrine how to count the resul

4条回答
  •  无人及你
    2020-12-14 03:48

    For anyone looking for an answer about this, there is a good solution at: https://github.com/KnpLabs/KnpPaginatorBundle/blob/master/Resources/doc/manual_counting.md

    $paginator = new Paginator;
    
    // determine the count of the entire result set
    $count = $entityManager
        ->createQuery('SELECT COUNT(c) FROM Entity\CompositeKey c')
        ->getSingleScalarResult();
    
    // create a query to be used with the paginator, and specify a hint
    $query = $entityManager
         ->createQuery('SELECT c FROM Entity\CompositeKey c')
         ->setHint(
            'knp_paginator.count', 
            $count
        );
    
    // paginate, and set "disctinct" option to false
    $pagination = $paginator->paginate(
        $query, 
        1, 
        10, 
        array(
            'distinct' => false,
        )
    );
    

    Basically, what you are doing, is you are creating your own 'count' query and instruct knp paginator to use this.

提交回复
热议问题