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:
<?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()
->createQuery('
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?
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')
->getRepository('YourBundle:Article')
->findBy(array(), array('id' => 'DESC'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With