Pagination using Doctrine and ZF2

亡梦爱人 提交于 2020-01-11 19:42:30

问题


i am trying to implement Pagination Using ZF2 and Doctrine.

What i am trying to do here is to fetch data from An associated table lets say 'xyz'.

Where as my categories table is doing one to many self referencing on its own PK. MY catgories tables has following feilds

  • ID (PK)
  • Created_at
  • Category_id (self referencing PK)

My XYZ table lets say it is called Name table has

  • ID (PK)
  • Category_id(FK)
  • name
  • Detail

This is what i am trying to do to fetch data

      public function allSubcategories($id, $column, $order) {
 $repository = $this->entityManager->getRepository('Category\Entity\Category');
    $queryBuilder = $repository->createQueryBuilder('category');
    $queryBuilder->distinct();
    $queryBuilder->select('category');

    $queryBuilder->join('Category\Entity\CategoryName', 'category_name', 'WITH', 'category.id = category_name.category');
    $queryBuilder->orderBy("category.status");
    $q = $queryBuilder->getDql();
    return $query = $this->entityManager->createQuery($q);
}

And in my controller this is what i am doing

      public function subcategoryAction() {

    ///////////////////////////InPut Params Given for the pagination
    $category_id = (int) $this->params()->fromRoute('id', 0);
    $page = (int) $this->params()->fromRoute('page', 0);
    $column = $this->params()->fromQuery('column');
    $order = $this->params()->fromQuery('order');



    $categoryModel = $this->getServiceLocator()->get('Category');
    $categoryModel->category = $category_id;


    $perPage = 10;
    $request = $this->getRequest();
    if ($request->isGet()) {
        $view = new ViewModel();
        $query = $categoryModel->allSubcategories($category_id, $column, $order);

        $paginator = new ORMPaginator($query);

        $paginator = new \Zend\Paginator\Paginator(new
                        \Zend\Paginator\Adapter\ArrayAdapter(array($paginator)));
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage(2);

    }

    return array('id' => $category_id, 'view' => $paginator);
}

Now i am not getting results with pagination implemented can some 1 guide me about what i am missing?


回答1:


You are using the wrong paginator there. Instead, you can use the one by DoctrineORMModule ( see DoctrineORMModule\Paginator\Adapter\DoctrinePaginator).

It may not be very obvious, but the logic is similar to what you already wrote:

use DoctrineORMModule\Paginator\Adapter\DoctrinePaginator as PaginatorAdapter;
use Doctrine\ORM\Tools\Pagination\Paginator as ORMPaginator;
use Zend\Paginator\Paginator as ZendPaginator;

$query = $categoryModel->allSubcategories($category_id, $column, $order);

$paginator = new ZendPaginator(new PaginatorAdapter(new ORMPaginator($query)));


来源:https://stackoverflow.com/questions/15068503/pagination-using-doctrine-and-zf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!