Merge pull request #3467 from halkyon/distinct_orm

Adding ability to change query distinct on DataList and DataQuery
This commit is contained in:
Stig Lindqvist 2014-09-04 14:08:40 +12:00
commit 9f3506bf02
4 changed files with 47 additions and 2 deletions

View File

@ -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.

View File

@ -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.
*

View File

@ -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());

View File

@ -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');
}
}