Merge remote-tracking branch 'refs/remotes/scienceninjas/story/sort-by-relation-on-datalist'

This commit is contained in:
Sam Minnee 2012-01-10 17:33:04 +13:00
commit d296a9411d
3 changed files with 32 additions and 4 deletions

View File

@ -194,7 +194,7 @@ class DataList extends ViewableData implements SS_List {
if(is_array($argumentArray)){
$sort = array();
foreach($argumentArray as $column => $direction) {
$sort []= '"'.$column.'" '.$direction;
$sort[]= ''.$this->getRelationName($column).' '.$direction;
}
$this->dataQuery->sort(implode(',', $sort));
return $this;
@ -253,6 +253,23 @@ class DataList extends ViewableData implements SS_List {
return $this;
}
/**
* Translates a Object relation name to a Database name and apply the relation join to
* the query
*
* @param string $field
* @return string
*/
public function getRelationName($field) {
if(strpos($field,'.') === false) {
return '"'.$field.'"';
}
$relations = explode('.', $field);
$fieldName = array_pop($relations);
$relationModelName = $this->dataQuery->applyRelation($field);
return '"'.$relationModelName.'"."'.$fieldName.'"';
}
/**
* Translates the comparisator to the sql query
*

View File

@ -181,13 +181,13 @@ class DataQuery {
$columnParts = explode(' ', $col);
if (count($columnParts) == 2) {
$dir = $columnParts[1];
$col = $columnParts[0];
$dir = $columnParts[1];
} else {
$dir = '';
$dir = 'ASC';
}
$orderByFields[$ob] = $col;
$orderByFields[$ob] = $col . ' ' . $dir;
$col = str_replace('"', '', $col);
$parts = explode('.', $col);

View File

@ -471,4 +471,15 @@ class DataListTest extends SapphireTest {
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Joe');
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
}
/**
*
*/
public function testSortByRelation() {
$list = DataList::create("DataObjectTest_TeamComment");
$list = $list->sort(array('Team.Title' => 'DESC'));
$this->assertEquals(3, $list->count());
$this->assertEquals(2, $list->first()->TeamID, 'First comment should be for Team 2');
$this->assertEquals(1, $list->last()->TeamID, 'Last comment should be for Team 1');
}
}