开发者

Limitting Rows For Doctrine FindAll Method

开发者 https://www.devze.com 2023-03-23 18:48 出处:网络
i am trying to limit rows which return from doctrine\'s FindAll method. public function getActiveUsersByPoint($limit = 100){

i am trying to limit rows which return from doctrine's FindAll method.

public function getActiveUsersByPoint($limit = 100){
    $users = $this->userRepository->findAll();

    return $users;
}

This code work but i can't use $limit variable for limitting results. How can i 开发者_运维知识库done this ?


The EntityRepository#findBy() method additionally accepts orderings, limit and offset as second to fourth parameters:

$tenUsers = $em->getRepository('MyProject\Domain\User')
               ->findBy(
                   array('age' => 20),        // $where 
                   array('name' => 'ASC'),    // $orderBy
                   10,                        // $limit
                   0                          // $offset
                 );


In order to find all results, you should pass an empty array to the findBy method, I think it is what you pretend:

$users= $em->userRepository->findBy(
            array(),
            array('id' => 'DESC'),
            10,
            0
        );

First param is an empty array, which it is equivalent to findAll(), then the order (I put id as sample), then the limit and finally the offset.


If your question is for Doctrine 1.x, FindAll means "find all". To limit the results, use DQL:

$q = Doctrine_Query::create()
  ->from('UserRepository')
  ->limit($limit);
$users = $q->execute();
0

精彩评论

暂无评论...
验证码 换一张
取 消