Symfony Doctrine Pagination

时间:2015-12-25 14:58:53

标签: php symfony doctrine-orm

我有实体用户,示例计数为90355,我需要100个用户,并对此用户执行逻辑操作。接下来100使用,这是我有但当我发现所有我的服务器下拉列表如何解决这个问题?

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;
}

这次迭代怎么办?

4 个答案:

答案 0 :(得分:0)

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

这是它的工作方式,对我来说这是最好的做法!也许不适合所有人!

  1. 请求转到控制器
  2. 控制器呼叫服务
  3. 使用Trait
  4. 服务规范化的请求参数
  5. 服务从存储库中提取数据
  6. 存储库将结果返回给服务
  7. 服务将结果传递给工厂
  8. 工厂创建结果模型
  9. 工厂将结果模型返回服务
  10. 服务将结果模型返回给控制器
  11. <强> 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);
        }
    }
    

    PAGER TRAIT

    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);
        }
    }
    

答案 1 :(得分:0)

我把这个例子从我发现的几件事中扔了出来,似乎有效。这是非常基本的,但这是一个开始。

回答:Doctrine2 Paginator getting total results

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

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$username = $_POST['username'];

$flen = strlen($firstname);
$llen = strlen($lastname);
$c = $flen + $llen;

if ($flen>0) {
  $fname = "firstname = '$firstname'" ;
}

if ($c=2) {
  $com =  "," ;
}

if ($llen>0) {
  $lname = "lastname = '$lastname'" ;
}

$sql = "update employee set " . $fname . $com . $lname . "
where username = '$username' ";

    if ($conn->query($sql) === TRUE) {
        echo "Record updated successfully";
    }

    else{
    echo "error: $sql. " . mysqli_error($conn);
    }
mysqli_close($conn);
?>

答案 2 :(得分:-1)

这是未经测试的,但我认为这应该有效:

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!");  

}

答案 3 :(得分:-1)

我如何在 symfony 上进行分页

/**
 * @Route("", name="admin_number_pool", methods={"GET"})
 */
public function index(NumberPoolRepository $repo, NumberPoolCategoryRepository $categories, Request $request): Response
{
    $page = $request->query->get('page', 1);
    $limit = 10;
    $pagesCount = ceil(count($repo->findAll()) / $limit);
    $pages = range(1, $pagesCount);
    $pools = $repo->findBy([], [], $limit, ($limit * ($page - 1)));


    return $this->render('admin/number-pool/index.html.twig', [
        'numberPools' => $pools,
        'categories' => $categories->findAll(),
        'pages' => $pages,
        'page' => $page,
    ]);
}