Mr. Smith Mr. Smith - 1 year ago 400
PHP Question

Symfony2, Doctrine2, findBy() order not working

I created a repository for my Articles entity and I'm trying to get all values ordered by ID DESC. But, I'll get everytime values ordered by id ASC. Here is my ArticleRepository.php:


namespace Acme\BlogBundle\Entity;

use Doctrine\ORM\EntityRepository;

class ArticleRepository extends EntityRepository
public function findAll()
return $this->findBy(array(), array('id' => 'DESC'));

public function findOneBySlug($slug)
$query = $this->getEntityManager()
SELECT p FROM AcmePagesBundle:Article a
WHERE a.slug = :slug
->setParameter('slug', $slug);

try {
return $query->getSingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
return false;

Any ideas?

Answer Source

The Syntax looks good. This should provide a set of well ordered articles.

First, make sure the @Entity annotation is set with the right Repository class within the Article Entity,

 * @ORM\Entity(repositoryClass="...");
class Article
    // ...

Also, if you want to use native helpers, you've just to call findBy from the ArticleRepository within your controller,

 $articles = $this->get('doctrine')
      ->findBy(array(), array('id' => 'DESC'));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download