15

I want to make a fetchAll() with limit ? Do you know if it's possible with the entity manager of symfony2 ?

My current code (Fetch all, without limit):

$repository = $this->getDoctrine()->getRepository('MyBundle:Download');
$product    = $repository->findAll();

Thanks you all. Best regards,

EDIT:

$em = $this->getDoctrine()->getRepository('MyBundle:Download');
$ouput = $em->findBy(array(), array('id' => 'DESC'),5);

Return the 5 last rows.

Thanks all.

Dimitri
  • 922
  • 2
  • 13
  • 34

1 Answers1

58

It's often instructive to check the source code.

Doctrine\ORM\EntityRepository 

public function findAll()
{
    return $this->findBy(array());
}
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
    $persister = $this->_em->getUnitOfWork()->getEntityPersister($this->_entityName);

    return $persister->loadAll($criteria, $orderBy, $limit, $offset);
}
Cerad
  • 48,157
  • 8
  • 90
  • 92
  • 6
    +1 for not giving the solution straight away, rather pointing the OP to the place where he can find a) the actual answer and b) a broader understanding of the context of his question – Jivan Feb 01 '14 at 15:05