phpsymfonypaginationsymfony4knppaginator

Can we create a pagination based on 2 entities in Symfony 4 with KnpPaginatorBundle?


I couldn’t find anywhere on the web if KnpPaginatorBundle can handle a pagination based on 2 (or more) entities.

Say I have a social page on my website where I would like to list all reddits and twitter posts from last week on a given topic. I have been scrapping and storing that data in my DB.

Now I would like to display it in a convenient way with the pagination bundle. To do so, I would like to merge 50/50 a set of entities from, on one hand, reddit, and on the other hand, twitter.

Is this possible to do? If not what would you recommend doing to paginate a set of 2 different entities displayed together ?

Note that both entities are linked to another entity Topic with a ManyToMany but I dont think knppaginatorbundle can handle pagination based on a relational entity (can it do a left join with offset and maxresult? not sure)

Thanks!


Solution

  • I tried something like this in one of my projects :

    {
         $em = $this->getDoctrine()->getManager();   
            $query = "SELECT f1,f2,f3 FROM table1
        UNION
        SELECT f1,f2,f3 FROM table2";
        $statement = $em->getConnection()->prepare($query);
                $statement->execute();  
            $paginator  = $this->get('knp_paginator');
            $pagination = $paginator->paginate(
                $query, /* query NOT result */
                $request->query->getInt('page', 1)/*page number*/,
                10/*limit per page*/
            );
    
            // parameters to template
            return $this->render('...', array('pagination' => $pagination));
        }