问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/4584484/how-to-order-by-multiple-columns-with-propel