mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX: Fixed DataList arrayaccess.
This commit is contained in:
parent
8db236f444
commit
7efd19e7cb
@ -400,6 +400,41 @@ class DataList extends DataObjectSet {
|
||||
function removeDuplicates() {
|
||||
user_error("Can't call DataList::removeDuplicates() because its data comes from a specific query.", E_USER_ERROR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Necessary for interface ArrayAccess. Returns whether an item with $key exists
|
||||
* @param mixed $key
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists($key) {
|
||||
return ($this->getRange($key, 1)->First() != null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Necessary for interface ArrayAccess. Returns item stored in array with index $key
|
||||
* @param mixed $key
|
||||
* @return DataObject
|
||||
*/
|
||||
public function offsetGet($key) {
|
||||
return $this->getRange($key, 1)->First();
|
||||
}
|
||||
|
||||
/**
|
||||
* Necessary for interface ArrayAccess. Set an item with the key in $key
|
||||
* @param mixed $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function offsetSet($key, $value) {
|
||||
throw new Exception("Can't alter items in a DataList using array-access");
|
||||
}
|
||||
|
||||
/**
|
||||
* Necessary for interface ArrayAccess. Unset an item with the key in $key
|
||||
* @param mixed $key
|
||||
*/
|
||||
public function offsetUnset($key) {
|
||||
throw new Exception("Can't alter items in a DataList using array-access");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -1098,6 +1098,15 @@ class DataObjectTest extends SapphireTest {
|
||||
$this->assertTrue(DataObject::get("DataObjectTest_SubTeam")->canSortBy("Title"));
|
||||
$this->assertTrue(DataObject::get("DataObjectTest_SubTeam")->canSortBy("SubclassDatabaseField"));
|
||||
}
|
||||
|
||||
function testDataListArrayAccess() {
|
||||
$list = DataObject::get("DataObjectTest_Team")->sort("Title");
|
||||
|
||||
$this->assertEquals("Subteam 1", $list[0]->Title);
|
||||
$this->assertEquals("Subteam 3", $list[2]->Title);
|
||||
$this->assertEquals("Team 2", $list[4]->Title);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user