gpt4 book ai didi

php - Symfony 学说分页

转载 作者:行者123 更新时间:2023-12-04 00:07:16 24 4
gpt4 key购买 nike

我有实体用户,例如计数 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;
}

这个迭代怎么做?

最佳答案

我把这个例子从我发现的一些东西中结合起来,它似乎有效。这是非常基本的,但它是一个开始。

所以答案:Doctrine2 Paginator getting total results

Symfony 文档: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];
}

关于php - Symfony 学说分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34463859/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com