mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Fixed DataList::find() for find by ID and find multiple times.
This commit is contained in:
parent
ec73555db4
commit
5ed14915bb
@ -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();
|
||||
}
|
||||
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user