mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
MINOR DataList::sort() can sort by relation name
This commit is contained in:
parent
20554b1bf9
commit
f59d11c91e
@ -182,7 +182,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;
|
||||
@ -241,6 +241,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
|
||||
*
|
||||
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user