Created
July 20, 2010 18:15
-
-
Save bshaffer/483322 to your computer and use it in GitHub Desktop.
order your result set by an array
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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