Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limitting Rows For Doctrine FindAll Method

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 ?

like image 942
mTuran Avatar asked Jul 28 '11 13:07

mTuran


2 Answers

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
                 );
like image 91
Sina Miandashti Avatar answered Sep 21 '22 04:09

Sina Miandashti


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.

like image 36
Marcos Labad Avatar answered Sep 24 '22 04:09

Marcos Labad