Skip to content

Instantly share code, notes, and snippets.

@bshaffer
Created July 20, 2010 18:15
Show Gist options
  • Save bshaffer/483322 to your computer and use it in GitHub Desktop.
Save bshaffer/483322 to your computer and use it in GitHub Desktop.
order your result set by an array
<?php
/*
* Extended Doctrine Query class providing a few additional functions
* for wrapping your where clauses more efficiently
*/
class Doctrine_Query_Extra extends Doctrine_Query
{
public function addOrderBy($orderby, $values = null)
{
if ($values !== null)
{
return $this->addOrderByWhereIn($orderby, $values);
}
return $this->_addDqlQueryPart('orderby', $orderby, true);
}
public function orderBy($orderby, $values = null)
{
if ($values !== null)
{
return $this->orderByWhereIn($orderby, $values);
}
return $this->_addDqlQueryPart('orderby', $orderby);
}
public function addOrderByWhereIn($field, $values)
{
return $this->orderByWhereIn($field, $values, true);
}
public function orderByWhereIn($field, $values, $append = false)
{
$params = explode(' ', $field);
$direction = isset($params[1]) ? $params[1] : 'ASC';
return $this
->_addDqlQueryPart('orderby', sprintf('FIELD(%s, "%s") = 0 %s', $field, implode('","', $values), $direction), $append)
->_addDqlQueryPart('orderby', sprintf('FIELD(%s, "%s") %s', $field, implode('","', $values), $direction), true);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment