2015-12-25 140 views
2

我有实体用户,例如计数90355,我需要100个用户获取,并与此用户进行逻辑操作。那么下100个使用,这是我有,但是当我找到所有我的服务器下拉如何解决这个问题?Symfony Doctrine分页

public function find() 
{ 
    $developers = $this->em->getRepository('ArtelProfileBundle:Users')->findBy(array(), array('id' => 'desc')); 
    foreach ($developers as $developer) { 
     $this->getApiFullContact($developer); 
    } 
    return true; 
} 

我觉得像这个函数,但setFirstResult和setMaxResults动态变量?

public function getPaginationUser() 
{ 
    $qb = $this->getEntityManager()->createQueryBuilder('d'); 
    $qb 
     ->select('d') 
     ->from('ArtelProfileBundle:Users', 'd') 
     ->setFirstResult(0) 
     ->setMaxResults(100) 
     ->orderBy('d.id', 'DESC') 
     ->getQuery() 
     ->getResult() 
    ; 
    $query = $qb->getQuery(); 
    $results = $query->getResult(); 

    return $results; 
} 

这个迭代怎么做?

+0

只是做'($页面-1)* 100;'其中$页是数要求,并用它来代替0在setFirstResult中。也没有理由做getQuery/getResult两次。只需删除前两个电话。 – JimL

+0

我怎么知道如何调用函数,也许第一个结果和最大结果动态变量?我更新我的问题 –

+0

看看https:// github。com/KnpLabs/KnpPaginatorBundle –

回答

0

这是未经测试,但我认为这应该工作:

public function find($offset = 0) 
{ 
    $developers = $this->em->getRepository('ArtelProfileBundle:Users')->findBy(array(), array('id' => 'desc'), 100, $offset); 

    foreach ($developers as $developer) { 
     $this->getApiFullContact($developer); 
    } 

    if (count($developers) < 100){ 
     $offset = $offset + 100; 
     $this->find($offset) 
    }  

    return new Response("finished!"); 

} 
0

完全工作的例子就是在这里 - >Using limit and offset in doctrine query builder for manual pagination。我只是给你你需要先了解的代码。

这是它的工作原理,对我来说这是最佳实践!也许不适合其他人!

  1. 请求转到控制器
  2. 控制器调用服务与性状
  3. 服务标准化请求参数
  4. 服务从信息库提取数据
  5. 库返回结果返回到服务
  6. 服务传递导致为出厂
  7. 厂创建结果型号
  8. 厂返回结果型号回到服务
  9. 服务回报合成模型回控制器

REPO

namespace Application\BackendBundle\Repository; 

use Doctrine\ORM\EntityRepository; 
use Doctrine\ORM\Query; 

class StudentRepository extends EntityRepository 
{ 
    /** 
    * @param string|null $name 
    * @param int   $limit 
    * @param int   $offset 
    * 
    * @returns array 
    */ 
    public function findPaginatedByName($name, $limit, $offset) 
    { 
     $qb = $this->createQueryBuilder('s'); 

     if ($name) { 
      $qb->where('s.name = :name')->setParameter('name', $name); 
     } 

     $qb->setMaxResults($limit)->setFirstResult($offset); 

     return $qb->getQuery()->getResult(Query::HYDRATE_SIMPLEOBJECT); 
    } 

    /** 
    * @param string|null $name 
    * 
    * @return int 
    */ 
    public function findPaginatedByNameCount($name) 
    { 
     $qb = $this->createQueryBuilder('s')->select('COUNT(s)'); 

     if ($name) { 
      $qb->where('s.name = :name')->setParameter('name', $name); 
     } 

     return $qb->getQuery()->getResult(Query::HYDRATE_SINGLE_SCALAR); 
    } 
} 

寻呼机特质

namespace Application\BackendBundle\Util; 

trait PagerTrait 
{ 
    public function getPage($page = 1) 
    { 
     if ($page < 1) { 
      $page = 1; 
     } 

     return floor($page); 
    } 

    public function getLimit($limit = 20) 
    { 
     if ($limit < 1 || $limit > 20) { 
      $limit = 20; 
     } 

     return floor($limit); 
    } 

    public function getOffset($page, $limit) 
    { 
     $offset = 0; 
     if ($page != 0 && $page != 1) { 
      $offset = ($page - 1) * $limit; 
     } 

     return $offset; 
    } 
} 

服务

namespace Application\BackendBundle\Service; 

use Application\BackendBundle\Factory\StudentFactoryInterface; 
use Application\BackendBundle\Model\Student\Result; 
use Application\BackendBundle\Repository\StudentRepository; 
use Application\BackendBundle\Util\PagerTrait; 

class StudentService implements StudentServiceInterface 
{ 
    use PagerTrait; 

    private $studentRepository; 
    private $studentFactory; 

    public function __construct(
     StudentRepository $studentRepository, 
     StudentFactoryInterface $studentFactory 
    ) { 
     $this->studentRepository = $studentRepository; 
     $this->studentFactory = $studentFactory; 
    } 

    /** 
    * @param string $name 
    * @param int $page 
    * @param int $limit 
    * 
    * @return Result 
    */ 
    public function get($name, $page, $limit) 
    { 
     $page = $this->getPage($page); 
     $limit = $this->getLimit($limit); 
     $offset = $this->getOffset($page, $limit); 
     $total = 0; 

     $result = $this->studentRepository->findPaginatedByName($name, $limit, $offset); 
     if ($result) { 
      $total = $this->studentRepository->findPaginatedByNameCount($name); 
     } 

     return $this->studentFactory->createStudentResult($result, $name, $page, $limit, $total); 
    } 
} 
0

我把这个例子从一些我已经发现的东西中解释出来,它似乎工作。这是非常基本的,但它是一个开始。

SO回答:Doctrine2 Paginator getting total results

Symfony的文档:Querying for Objects Using Doctrine's Query Builder

/** 
* @Route("https://stackoverflow.com/users/{page}", name="user_list", requirements={"page"="\d+"}) 
*/ 
public function getUsersByPage($page = 1) 
{ 
    // get entity manager 
    $em = $this->getDoctrine()->getManager(); 

    // get the user repository 
    $developers = $em->getRepository(Users::class); 

    // build the query for the doctrine paginator 
    $query = $developers->createQueryBuilder('u') 
         ->orderBy('d.id', 'DESC') 
         ->getQuery(); 

    //set page size 
    $pageSize = '100'; 

    // load doctrine Paginator 
    $paginator = new \Doctrine\ORM\Tools\Pagination\Paginator($query); 

    // you can get total items 
    $totalItems = count($paginator); 

    // get total pages 
    $pagesCount = ceil($totalItems/$pageSize); 

    // now get one page's items: 
    $paginator 
     ->getQuery() 
     ->setFirstResult($pageSize * ($page-1)) // set the offset 
     ->setMaxResults($pageSize); // set the limit 

    foreach ($paginator as $pageItem) { 
     // do stuff with results... 
     dump($pageItem); 
    } 

    // return stuff.. 
    return [$userList, $totalItems, $pageCount]; 
}