How would you write the following query in Zend framework 3 ?
SELECT * FROM table_name ORDER BY FIELD(field_name, 'a','b','c');
I tried
$select->order(new Expression("FIELD(field_name, 'a', 'b', 'c')"),field_name);
but not moves...
How would you write the following query in Zend framework 3 ?
SELECT * FROM table_name ORDER BY FIELD(field_name, 'a','b','c');
I tried
$select->order(new Expression("FIELD(field_name, 'a', 'b', 'c')"),field_name);
but not moves...
On
I think you made a mistake somewhere, make sure that you've:
Expression correctly with use.order(), there is none.Zend\Db\Sql\Expression is the correct way to extend a query with DBMS-specific expressions.
With the following test script I get your expected result:
$platform = new Zend\Db\Adapter\Platform\Mysql();
$select = new Zend\Db\Sql\Select();
$select->from(['t' => 'table_name']);
$select->order(
new Zend\Db\Sql\Expression(sprintf(
"FIELD(t.%s, 'a', 'b', 'c')",
$platform->quoteIdentifier('field_name')
))
);
var_dump($select->getSqlString($platform));
Result is:
string(81) "SELECT `t`.* FROM `table_name` AS `t` ORDER BY FIELD(t.`field_name`, 'a', 'b', 'c')"
Few notes here:
Zend namespace with Laminas when you using Laminas instead.quoteIdentifier() from the platform instance (you should be able to receive the platform from your table or DB instance).FIELD() (you can either do this in your application or on the DDL level).
Instead of using expression, can you give string to order method
If field name is a variable
If the issue still persists, then we can trace the issue by printing the query
dont remember the query exactly, you can try them