简体   繁体   中英

Where condition in Doctrine and Zend Framework 2 Pagination

I'm unable to make a where condition to work with Doctrine and Zend framework 2 pagination. My code is given below.

public function indexAction(){
    $em = $this->getEntityManager();
    $repository = $em->getRepository('Catalog\Entity\Category');
    $queryBuilder = $repository->createQueryBuilder('Category');
    $adapter = new DoctrinePaginator(new ORMPaginator($queryBuilder));

    $paginator = new Paginator($adapter);
    $paginator->setDefaultItemCountPerPage(9);

    $page = (int)$this->params()->fromQuery('page');
    if($page) $paginator->setCurrentPageNumber($page);
    $view = new ViewModel(
        array('paginator' => $paginator)
    );
    return $view;
}

I want to get Categories for which 'enabled' column value is 1.

The alternate way I got is create DQL query but I don't get the benifit of zend framework 2 pagination.

$repository->createQueryBuilder('Category')->where(array('enabled' => 1));

I want to use my top code way because it helps me to take full benefit of zend framework 2 pagination but how can I specify a where condition in it?

I solved it by using QueryBuilder of Doctrine. Find the code below to add condition along with Zend Framework 2 pagination.

public function indexAction(){
    $em = $this->getEntityManager();
    $repository = $em->getRepository('Catalog\Entity\Category');
    $queryBuilder = $repository->createQueryBuilder('category')
        ->where('category.enabled = 1')
        ->orderBy('category.sortOrder', 'ASC');
    $adapter = new DoctrinePaginator(new ORMPaginator($queryBuilder));

    $paginator = new Paginator($adapter);
    $paginator->setDefaultItemCountPerPage(9);

    $page = (int)$this->params()->fromQuery('page');
    if($page) $paginator->setCurrentPageNumber($page);
    $view = new ViewModel(
        array('paginator' => $paginator)
    );
    return $view;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM