mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
Merge branch '4' into 5.1
This commit is contained in:
commit
bf45b0c44b
@ -237,11 +237,11 @@ class UnsavedRelationList extends ArrayList implements Relation
|
|||||||
*/
|
*/
|
||||||
public function first()
|
public function first()
|
||||||
{
|
{
|
||||||
$item = reset($this->items);
|
$item = reset($this->items) ?: null;
|
||||||
if (is_numeric($item)) {
|
if (is_numeric($item)) {
|
||||||
$item = DataObject::get_by_id($this->dataClass, $item);
|
$item = DataObject::get_by_id($this->dataClass, $item);
|
||||||
}
|
}
|
||||||
if (!empty($this->extraFields[key($this->items)])) {
|
if ($item && !empty($this->extraFields[key($this->items)])) {
|
||||||
$item->update($this->extraFields[key($this->items)]);
|
$item->update($this->extraFields[key($this->items)]);
|
||||||
}
|
}
|
||||||
return $item;
|
return $item;
|
||||||
@ -254,8 +254,11 @@ class UnsavedRelationList extends ArrayList implements Relation
|
|||||||
*/
|
*/
|
||||||
public function last()
|
public function last()
|
||||||
{
|
{
|
||||||
$item = end($this->items);
|
$item = end($this->items) ?: null;
|
||||||
if (!empty($this->extraFields[key($this->items)])) {
|
if (is_numeric($item)) {
|
||||||
|
$item = DataObject::get_by_id($this->dataClass, $item);
|
||||||
|
}
|
||||||
|
if ($item && !empty($this->extraFields[key($this->items)])) {
|
||||||
$item->update($this->extraFields[key($this->items)]);
|
$item->update($this->extraFields[key($this->items)]);
|
||||||
}
|
}
|
||||||
return $item;
|
return $item;
|
||||||
|
@ -310,4 +310,20 @@ class UnsavedRelationListTest extends SapphireTest
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFirstAndLast()
|
||||||
|
{
|
||||||
|
$object = new UnsavedRelationListTest\TestObject();
|
||||||
|
$children = $object->Children();
|
||||||
|
|
||||||
|
$this->assertNull($children->first(), 'Empty UnsavedRelationList should return null for first item.');
|
||||||
|
$this->assertNull($children->last(), 'Empty UnsavedRelationList should return null for last item.');
|
||||||
|
|
||||||
|
$children->add($firstChild = $this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectA'));
|
||||||
|
$children->add($this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectB'));
|
||||||
|
$children->add($lastChild = $this->objFromFixture(UnsavedRelationListTest\TestObject::class, 'ObjectC'));
|
||||||
|
|
||||||
|
$this->assertEquals($firstChild, $children->first(), 'Incorrect first item in UnsavedRelationList.');
|
||||||
|
$this->assertEquals($lastChild, $children->last(), 'Incorrect last item in UnsavedRelationList.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user