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

select order by fields value

Hi, I wonder if I could select query order by fields through builder or critiera

SELECT * FROM fruit ORDER BY FIELD(name, 'Banana', 'Apple', 'Pear', 'Orange'),

thanks



15.2k

Thanks, I guess I didn't explain myself clear, I am familar with find/phql, but I want to use ORDER BY FIELD, so it always return same order I wanted. I was after something phql support with modelManager, I guess I have to use raw query to do so.

Hi,

you can do quiet everything:

using ORM: https://docs.phalcon.io/en/latest/reference/models.html#finding-records

using phql (a kind of high level query language): https://docs.phalcon.io/en/latest/reference/phql.html#selecting-records

using query builder: https://docs.phalcon.io/en/latest/reference/phql.html#creating-queries-using-the-query-builder

or even raw SQL: https://docs.phalcon.io/en/latest/reference/phql.html#using-raw-sql



43.9k

// ORM
// Get first 100 virtual robots ordered by name
$robots = Robots::find(
    array(
        "type = 'virtual'",
        "order" => "name",
        "limit" => 100
    )
);

//PHQL
$phql   = "SELECT * FROM Formula\Cars ORDER BY Formula\Cars.name";
$query  = $manager->createQuery($phql);

// and so on