Symfony学说分页 [英] Symfony Doctrine Pagination

查看:155
本文介绍了Symfony学说分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有实体用户,示例计数为90355,我需要获得100个用户并对该用户进行逻辑处理。然后是下100个用途,这是我所拥有的,但是当我找到所有我的服务器下拉菜单时,如何解决此问题?

I have entity users, example count 90355 and I need get by 100 users and do something logic with this user. then next 100 use, this is I have but when I findAll my server dropdown how to solved this problem ?

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动态变量?

I think like this function but setFirstResult and setMaxResults dynamics variable ?

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

此迭代如何进行?

推荐答案

我从发现的一些东西中把这个例子放到一起,似乎可行。

I threw this example together from a couple things I have found and it seems to work. This is pretty basic but it is a start.

SO答案: Doctrine2分页器获得总结果

Symfony文档:使用Doctrine的查询生成器查询对象

Symfony Docs: Querying for Objects Using Doctrine's Query Builder

/**
 * @Route("/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];
}

这篇关于Symfony学说分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆