How to order by multiple columns with propel
Asked Answered
W

2

5

I need to sort a query by 2 columns. Is this possible using propel?

i tried:

$c->addAscendingOrderByColumn(self::COL1);
$c->addAscendingOrderByColumn(self::COL2);

but the second call to addAscendingOrderByColumn overrides the first one.

Regards, Radu.

Wertz answered 3/1, 2011 at 12:38 Comment(0)
P
5

Unfortunately, if you are using Propel 1.4 (Symfony 1.4), it seems that you will have to switch to raw SQL there...

No answer in the (old) Symfony forum : http://oldforum.symfony-project.org/index.php/m/90447/

However, since Propel 1.5 it should work with the new syntax (Doctrine style). It's not detailed in Propel doc but when I test this, it works (gives ORDER BY author.name ASC, author.id DESC).

$authors = AuthorQuery::create()
  ->orderByName()
  ->orderById('desc')
  ->find();

Maybe consider upgrading to Propel 1.5.

Paucker answered 4/1, 2011 at 12:7 Comment(0)
A
6

I was struggling with this issue and after reading this post I have been almost gave up... but suddendly I found this solution:

$criteria->addAscendingOrderByColumn(ClassPeer::COLUMN1)->addAscendingOrderByColumn(ClassPeer::COLUMN2);

In this way the second call to addAscendingOrderByColumn will add the order to the first one.

Airliner answered 18/12, 2011 at 18:2 Comment(0)
P
5

Unfortunately, if you are using Propel 1.4 (Symfony 1.4), it seems that you will have to switch to raw SQL there...

No answer in the (old) Symfony forum : http://oldforum.symfony-project.org/index.php/m/90447/

However, since Propel 1.5 it should work with the new syntax (Doctrine style). It's not detailed in Propel doc but when I test this, it works (gives ORDER BY author.name ASC, author.id DESC).

$authors = AuthorQuery::create()
  ->orderByName()
  ->orderById('desc')
  ->find();

Maybe consider upgrading to Propel 1.5.

Paucker answered 4/1, 2011 at 12:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.