2013-06-24 31 views
0

我正在建立knp paginator,我的作曲家設置爲「knplabs/knp-paginator-bundle」:「2.3。*」但是它不適用於Query對象。Knp Paginator不適用於查詢

這裏是我的查詢:

public function getCommentariesByUser(User $user) { 
     $qb = $this->createQueryBuilder('c'); 
     $qb->innerJoin('c.item', 'i'); 

     $qb->andWhere($qb->expr()->eq('i.user', ':user')) 
       ->setParameter(':user', $user); 

     $qb->andWhere($qb->expr()->neq('c.user', ':user1')) 
       ->setParameter(':user1', $user); 

     return $qb->getQuery(); 
} 

如何,我稱其爲我的行動:

public function commentariesAction($page) { 
    $user = $this->getUser(); 
    $commentaryRepository = $this->getDoctrine()->getRepository('My\MyBundle\Entity\Commentary'); 
    $query = $commentaryRepository->getCommentariesByUser($user); 

    $paginator = $this->get('knp_paginator'); 
    $pagination = $paginator->paginate(
     $query, 
     $page/*page number*/, 
     15/*limit per page*/ 
    ); 

    return array('pagination' => $pagination); 
} 

它根本不顯示的結果,但是如果我把$查詢 - >執行( ) 有用。怎麼了?

+0

你是出於測試目的返回$分頁?我是KnpPaginatorBundle的新手,但我將其傳遞給視圖: 'return $ this-> render('MyBundle :: myTemplate.html.twig',array('pagination'=> $ pagination)); ' 然後,您可以使用類似於「{%for item in pagination%}」的內容來訪問這些項目。 – caponica

+0

另外,如果使用QueryBuilder而不是Query,它是否工作?即'return $ qb'而不是'getCommentariesByUser()'返回$ qb-> getQuery()'' – caponica

回答

0

這是目前可以使用分頁程序包的下一個組合上面的代碼工作:

"knplabs/knp-components": "1.2.1", 
"knplabs/knp-paginator-bundle": "~2.3" 
相關問題