mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge remote-tracking branch 'refs/remotes/scienceninjas/story/sort-by-relation-on-datalist'
This commit is contained in:
commit
d296a9411d
@ -194,7 +194,7 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
if(is_array($argumentArray)){
|
if(is_array($argumentArray)){
|
||||||
$sort = array();
|
$sort = array();
|
||||||
foreach($argumentArray as $column => $direction) {
|
foreach($argumentArray as $column => $direction) {
|
||||||
$sort []= '"'.$column.'" '.$direction;
|
$sort[]= ''.$this->getRelationName($column).' '.$direction;
|
||||||
}
|
}
|
||||||
$this->dataQuery->sort(implode(',', $sort));
|
$this->dataQuery->sort(implode(',', $sort));
|
||||||
return $this;
|
return $this;
|
||||||
@ -253,6 +253,23 @@ class DataList extends ViewableData implements SS_List {
|
|||||||
return $this;
|
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
|
* Translates the comparisator to the sql query
|
||||||
*
|
*
|
||||||
|
@ -181,13 +181,13 @@ class DataQuery {
|
|||||||
|
|
||||||
$columnParts = explode(' ', $col);
|
$columnParts = explode(' ', $col);
|
||||||
if (count($columnParts) == 2) {
|
if (count($columnParts) == 2) {
|
||||||
$dir = $columnParts[1];
|
|
||||||
$col = $columnParts[0];
|
$col = $columnParts[0];
|
||||||
|
$dir = $columnParts[1];
|
||||||
} else {
|
} else {
|
||||||
$dir = '';
|
$dir = 'ASC';
|
||||||
}
|
}
|
||||||
|
|
||||||
$orderByFields[$ob] = $col;
|
$orderByFields[$ob] = $col . ' ' . $dir;
|
||||||
$col = str_replace('"', '', $col);
|
$col = str_replace('"', '', $col);
|
||||||
$parts = explode('.', $col);
|
$parts = explode('.', $col);
|
||||||
|
|
||||||
|
@ -471,4 +471,15 @@ class DataListTest extends SapphireTest {
|
|||||||
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Joe');
|
$this->assertEquals('Joe', $list->first()->Name, 'First comment should be from Joe');
|
||||||
$this->assertEquals('Phil', $list->last()->Name, 'Last comment should be from Phil');
|
$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