anubis
anubis

Reputation: 1505

symfony doctrine order by

I'm trying to use doctrine and take the values ordered, but I cannot.

I try so:

$articlesB = $this
                ->getDoctrine()
                ->getManager()
                ->getRepository('theBundle:Article')
                ->findAll(array('date' => 'ASC'));

Do you know howw to take this values ordered by date? A column is named date and take all the dates. I want to have this orderer.

Thanks Best regards

Upvotes: 2

Views: 8455

Answers (2)

Cerad
Cerad

Reputation: 48865

Use findBy instead of findAll with an empty array for the first argument (selection criteria) and your sorting array as the second argument.

$articlesB = $this
            ->getDoctrine()
            ->getManager()
            ->getRepository('theBundle:Article')
            ->findBy(array(),array('date' => 'ASC'));

In this case I looked at the actual source code. You would think that findAll() would work but nope. It never passes the sorting criteria on.

Upvotes: 5

geoB
geoB

Reputation: 4704

You'll need to create an ArticleRepostory and in it:

public function getOrderedArticles()
{
    $return $this->getEntityManager()
            ->createQuery(
            "SELECT a FROM theBundle:Article a "
            . "ORDER BY a.date ASC"
    );
}

so that your controller could do

$articlesB = $this
                ->getDoctrine()
                ->getManager()
                ->getRepository('theBundle:Article')
                ->getOrderedArticles();

Upvotes: 0

Related Questions