BUGFIX: Fixed DataList::find() for find by ID and find multiple times.

This commit is contained in:
Sam Minnee 2011-10-29 17:12:02 +13:00
parent ec73555db4
commit 5ed14915bb
2 changed files with 25 additions and 1 deletions

View File

@ -253,7 +253,16 @@ class DataList extends ViewableData implements SS_List {
* Find an element of this DataList where the given key = value
*/
public function find($key, $value) {
return $this->where("\"$key\" = '" . Convert::raw2sql($value) . "'")->First();
$clone = clone $this;
if($key == 'ID') {
$baseClass = ClassInfo::baseDataClass($this->dataClass);
$SQL_col = "\"$baseClass\".\"$key\"";
} else {
$SQL_col = "\"$key\"";
}
return $clone->where("$SQL_col = '" . Convert::raw2sql($value) . "'")->First();
}

View File

@ -175,4 +175,19 @@ class DataListTest extends SapphireTest {
$this->assertEquals("Subteam 3", $list[2]->Title);
$this->assertEquals("Team 2", $list[4]->Title);
}
function testFind() {
$list = DataList::create("DataObjectTest_Team");
$record = $list->find('Title', 'Team 1');
$this->assertEquals($this->idFromFixture('DataObjectTest_Team', 'team1'), $record->ID);
}
function testFindById() {
$list = DataList::create("DataObjectTest_Team");
$record = $list->find('ID', $this->idFromFixture('DataObjectTest_Team', 'team1'));
$this->assertEquals('Team 1', $record->Title);
// Test that you can call it twice on the same list
$record = $list->find('ID', $this->idFromFixture('DataObjectTest_Team', 'team2'));
$this->assertEquals('Team 2', $record->Title);
}
}