From 5ed14915bb02dd255cfba3c16d2097446121bb7c Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Sat, 29 Oct 2011 17:12:02 +1300 Subject: [PATCH] BUGFIX: Fixed DataList::find() for find by ID and find multiple times. --- model/DataList.php | 11 ++++++++++- tests/model/DataListTest.php | 15 +++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/model/DataList.php b/model/DataList.php index 0c1715dfe..e26774094 100644 --- a/model/DataList.php +++ b/model/DataList.php @@ -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(); } diff --git a/tests/model/DataListTest.php b/tests/model/DataListTest.php index 90f13d8e1..351fb0d8e 100644 --- a/tests/model/DataListTest.php +++ b/tests/model/DataListTest.php @@ -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); + } } \ No newline at end of file