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.

Radu Dragomir
  • 660
  • 2
  • 9
  • 35

2 Answers2

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.

macgyver
  • 1,279
  • 2
  • 9
  • 16
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.

Maxime Pacary
  • 22,336
  • 11
  • 85
  • 113