Zend DB Select : ORDER BY FIELD('id',some_array) - how?
Asked Answered
G

3

16

How would you write the following query in Zend framework?

SELECT * FROM table_name ORDER BY FIELD(field_name, 'Small','Medium','Large');

I just need the "Order by" part :)

Thanks!

Goodsized answered 7/7, 2011 at 14:15 Comment(0)
F
28

What about this:

      $db = Zend_Db_Table::getDefaultAdapter();

      $select = $db->select();

      $select->from('table_name')
              ->order(new Zend_Db_Expr("FIELD(field_name, 'Small','Medium','Large')"));


      var_dump($select->assemble());

Results in:

string 'SELECT `table_name`.* FROM `table_name` ORDER BY FIELD(field_name, 'Small','Medium','Large')' (length=92)
Franchot answered 7/7, 2011 at 14:21 Comment(0)
S
3
$select->order(new Zend_Db_Expr('FIELD(field_name, 'Small','Medium','Large')'));
Seize answered 7/7, 2011 at 14:19 Comment(0)
E
2

I think you should do:

$db = Zend_Db::factory( ...options... );
$select = $db->select()
 ->from(table_name)
 ->order(new Zend_Db_Expr("FIELD(field_name, 'Small','Medium','Large')")));
Estep answered 7/7, 2011 at 14:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.