We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model order by newest at bottom

How do I make a SQL that fetches the newest rows, but reorder them, so that the newest is at the bottom of the output

like:

6 5 4 3 2 1

Instead of

1 2 3 4 5 6

How can i do this?



85.5k
edited Mar '17
Model::find([
    "order" => "id desc" // or when creating records add field create_at = time(), and order by it ASC
]);

?

I think you misunderstod me, i want to fetch the newest records, but then reorder them, so the newest is at the bottom and the oldest on op

edited Mar '17
$resultSet = $this->modelsManager->createQuery("
SELECT * FROM (SELECT * FROM [Robots] ORDER BY id DESC LIMIT 10) tmp ORDER BY id ASC
")->execute();

Not sure if this will work with PHQL, but that is how you'd do it with mysql. You could also convert your original ResultSet to array, and issue array_reverse, but that's not a robust solution performance-wise



43.9k
edited Mar '17

Hi,

if I understand, you want to sort your resultset in the view environment. There is a "sort" filter implemented in volt, see: https://docs.phalcon.io/en/latest/reference/volt.html#filters, but it will sort your items in an ASC order

I think, you will have to create your own filter to achieve what you exactly need, see https://docs.phalcon.io/en/latest/reference/volt.html#id2