mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
Adding ability to change query distinct on DataList and DataQuery
This commit is contained in:
parent
c112c1c69f
commit
151b7e9876
@ -223,7 +223,18 @@ class DataList extends ViewableData implements SS_List, SS_Filterable, SS_Sortab
|
||||
$query->limit($limit, $offset);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return a new DataList instance with distinct records or not
|
||||
*
|
||||
* @param bool $value
|
||||
*/
|
||||
public function distinct($value) {
|
||||
return $this->alterDataQuery(function($query) use ($value){
|
||||
$query->distinct($value);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a new DataList instance as a copy of this data list with the sort
|
||||
* order set.
|
||||
|
@ -549,6 +549,17 @@ class DataQuery {
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set whether this query should be distinct or not.
|
||||
*
|
||||
* @param bool $value
|
||||
* @return DataQuery
|
||||
*/
|
||||
public function distinct($value) {
|
||||
$this->query->setDistinct($value);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an INNER JOIN clause to this query.
|
||||
*
|
||||
|
@ -121,7 +121,18 @@ class DataListTest extends SapphireTest {
|
||||
// $check = $list->limit(null, 2);
|
||||
// $this->assertEquals(1, $check->count());
|
||||
}
|
||||
|
||||
|
||||
public function testDistinct() {
|
||||
$list = DataObjectTest_TeamComment::get();
|
||||
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql(), 'Query is set as distinct by default');
|
||||
|
||||
$list = $list->distinct(false);
|
||||
$this->assertNotContains('SELECT DISTINCT', $list->dataQuery()->sql(), 'Query does not contain distinct');
|
||||
|
||||
$list = $list->distinct(true);
|
||||
$this->assertContains('SELECT DISTINCT', $list->dataQuery()->sql(), 'Query contains distinct');
|
||||
}
|
||||
|
||||
public function testDataClass() {
|
||||
$list = DataObjectTest_TeamComment::get();
|
||||
$this->assertEquals('DataObjectTest_TeamComment',$list->dataClass());
|
||||
|
@ -155,6 +155,18 @@ class DataQueryTest extends SapphireTest {
|
||||
$result = $query->column('Title');
|
||||
$this->assertEquals(array('First', 'Second', 'Last'), $result);
|
||||
}
|
||||
|
||||
public function testDistinct() {
|
||||
$query = new DataQuery('DataQueryTest_E');
|
||||
$this->assertContains('SELECT DISTINCT', $query->sql(), 'Query is set as distinct by default');
|
||||
|
||||
$query = $query->distinct(false);
|
||||
$this->assertNotContains('SELECT DISTINCT', $query->sql(), 'Query does not contain distinct');
|
||||
|
||||
$query = $query->distinct(true);
|
||||
$this->assertContains('SELECT DISTINCT', $query->sql(), 'Query contains distinct');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user