This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

Problem with knp paginator on Symfony 2.4

Discuss Symfony 2 bundles here

Problem with knp paginator on Symfony 2.4

by francoca87 » Thu Oct 09, 2014 8:59 pm

Hi, I'm having a very rare problem using knp paginator.

The issue occur because somehow when I use the bundle to paginate a query, the sort and direction hints are being saved somewhere that is unknown to me.
When I use again the bundle to paginate another query, I'm getting an error because the paginator is trying to sort the query results by the sort value of the old pagination.

This is my controller:

Code: Select all
public function studentCareers(UrRStudents $student)
   {
      $request = $this->getRequest();
      $customSearchBy = $request->get('customSearchBy', '');
      
      $entityManager = $this->get('doctrine.orm.entity_manager');
      
      $page = $this->get('request')->query->get('page', 1);
      
      if ($request->request->get('itemsPerPage')) {
         $itemsPerPage = $request->request->get('itemsPerPage', 20);
      } else {
         $itemsPerPage = $request->query->get('itemsPerPage', 20);
      }
      
      $careerStudentRepository = $entityManager->getRepository('URANDRSugarCRMBundle:UrRCareersUrRStudentsC');
      $degreeStudentRepository = $entityManager->getRepository('URANDRSugarCRMBundle:UrRDegreesUrRStudentsC');
      $collegeStudentRepository = $entityManager->getRepository('URANDRSugarCRMBundle:UrRCollegesUrRStudentsC');
      
      $paginationService = new ServicePaginator($this->container, $this->get('knp_paginator'));
      
      $careerscoresCareersRepository = $entityManager->getRepository('URANDRSugarCRMBundle:UrRCareerscoresUrRCareersC');
      $careerscoresCareers = $careerscoresCareersRepository->getStudentRecomendedCareers($student->getId(), $customSearchBy);
      $careerscoresCareersPaginated = $paginationService->paginate($careerscoresCareers, $page, $itemsPerPage, $paginationService->getPaginationOptions($request, 'careerScores.matchScore', 'desc', $careersListSort))
      
      $careersSortListRepository = $entityManager->getRepository('URANDRSugarCRMBundle:UrRStudentcareerlistsort');
      $careersListSort = $careersSortListRepository->getSortList($student, $this->getUser());
      
      $studentRepository = $this->getDoctrine()->getRepository('URANDRSugarCRMBundle:UrRStudents');
      $students = $studentRepository->getStudentsCollection('all', $customSearchBy);
      $studentsPaginated = $paginationService->paginate($students, $page, $itemsPerPage, $paginationService->getPaginationOptions($request, 'students.dateEntered', 'desc'));
      
      return $this->render(
                  'URANDRAdvisorBundle:dashboards:student_careers.html.twig', array(
               'studentsList' => $studentsPaginated,
               'careerscoresCareers' => $careerscoresCareersPaginated
                  )
      );
   }


This is my service paginator:

Code: Select all
<?php

namespace URANDR\Bundle\AdvisorBundle\Service;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Knp\Component\Pager\Paginator;
use Symfony\Component\HttpFoundation\Request;
use URANDR\Bundle\SugarCRMBundle\Entity\Students;
use URANDR\Bundle\SugarCRMBundle\Entity\Users;
use URANDR\Bundle\AdvisorBundle\Service\ServiceSortLists;
use URANDR\Bundle\UtilsBundle\Service\ServiceException;

class ServicePaginator
{

   protected $container = null;
   protected $paginatorEngine = null;
   protected $logEngine = null;

   /**
    * Constructor
    *
    * @param Symfony\Component\DependencyInjection\ContainerInterface $container
    * @param Knp\Component\Pager\Paginator $paginator
    */
   public function __construct(ContainerInterface $container, \Knp\Component\Pager\Paginator $paginator)
   {
      $this->container = $container;
      $this->paginatorEngine = $paginator;
      $this->logEngine = $container->get('monolog.logger.admin_log');
   }

   /**
    * Returns a paginated query
    *
    * @param \Doctrine\ORM\Query $query
    * @param int $page
    * @param int $itemsPerPage
    * @param array $options
    * @return type
    */
   public function paginate(\Doctrine\ORM\Query $query, $page = 1, $itemsPerPage = 20, $options = array())
   {
      try{
         if(!isset($options['pageParameterName']) || $options['pageParameterName'] == ''){
            $options = $this->generatePageName($options);
         }
         $queryPaginated = $this->paginatorEngine->paginate(
               $query, $page, $itemsPerPage, $options
         );
      } catch (\Exception $e) {
         if(!is_null($this->logEngine)){
            $exception = new ServiceException(
                  $this->logEngine,
                  $e->getMessage(),
                  $e->getCode(),
                  'Error when paginating a collection.');
            $exception->throwException(false);
         }
         $options = $this->generatePageName($options);
         $queryPaginated = $this->paginatorEngine->paginate(
               $query, $page, $itemsPerPage, $options
         );
      }
      
      return $queryPaginated;
   }
   
   /**
    * Returns an array with for the paginator
    *
    * @param \Symfony\Component\HttpFoundation\Request $request
    * @param string $defaultSort
    * @param string $defaultDirection
    * @param mixed $list
    * @return array
    */
   public function getPaginationOptions(Request $request, $defaultSort, $defaultDirection, $list=null){
      $arrayOptions = array();
      
      if(!$request->query->get('sort', null) || !$request->query->get('direction', null)){
         if($list && $list->getSort() && $list->getDirection()){
            $arrayOptions['defaultSortFieldName'] = $list->getSort();
            
            $arrayOptions['defaultSortDirection'] = $list->getDirection();
         }else{
            $arrayOptions['defaultSortFieldName'] = $defaultSort;
            
            $arrayOptions['defaultSortDirection'] = $defaultDirection;
         }
         
      }else{
         $arrayOptions['defaultSortFieldName'] = $request->query->get('sort');
         
         $arrayOptions['defaultSortDirection'] = $request->query->get('direction');
      }
      
      $arrayOptions['pageParameterName'] = $request->query->get('pageParameterDefault');
      
      $arrayOptions = $this->generatePageName($arrayOptions);
      
      return $arrayOptions;
   }
   
   /**
    * Generate an uniqid for the pagination page
    *
    * @param array $options
    * @return array
    */
   protected function generatePageName($options = array())
   {
      $options['pageParameterName'] = uniqid();
      return $options;
   }

}


Thank you for your help!
francoca87
Junior Member
 
Posts: 1
Joined: Thu Oct 09, 2014 7:26 pm