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!

srgb
  • 4,783
  • 6
  • 29
  • 45

3 Answers3

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)
Marcin
  • 215,873
  • 14
  • 235
  • 294
3
$select->order(new Zend_Db_Expr('FIELD(field_name, 'Small','Medium','Large')'));
Alex Pliutau
  • 21,392
  • 27
  • 113
  • 143
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')")));
Nicola Peluchetti
  • 76,206
  • 31
  • 145
  • 192