From e28af9a5a7d41fe6bf240a3f51165f95acc5a98a Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Mon, 4 Dec 2023 11:28:51 +0000 Subject: [PATCH 1/2] FIX: UnsavedRelationList first/last to return null if list is empty (fixes #11083) --- src/ORM/UnsavedRelationList.php | 8 ++++---- tests/php/ORM/UnsavedRelationListTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/ORM/UnsavedRelationList.php b/src/ORM/UnsavedRelationList.php index 51cde920fd9..4a6a702f296 100644 --- a/src/ORM/UnsavedRelationList.php +++ b/src/ORM/UnsavedRelationList.php @@ -239,11 +239,11 @@ public function getIDList() */ public function first() { - $item = reset($this->items); + $item = reset($this->items) ?: null; if (is_numeric($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)]); } return $item; @@ -256,8 +256,8 @@ public function first() */ public function last() { - $item = end($this->items); - if (!empty($this->extraFields[key($this->items)])) { + $item = end($this->items) ?: null; + if ($item && !empty($this->extraFields[key($this->items)])) { $item->update($this->extraFields[key($this->items)]); } return $item; diff --git a/tests/php/ORM/UnsavedRelationListTest.php b/tests/php/ORM/UnsavedRelationListTest.php index 7a8955f2d42..5c6559c51c3 100644 --- a/tests/php/ORM/UnsavedRelationListTest.php +++ b/tests/php/ORM/UnsavedRelationListTest.php @@ -310,4 +310,20 @@ public function testColumn() ] ); } + + 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.'); + } } From 40b888eaf30f6c6899134f7fcbfb0705987516e1 Mon Sep 17 00:00:00 2001 From: Loz Calver Date: Mon, 4 Dec 2023 11:44:34 +0000 Subject: [PATCH 2/2] FIX: UnsavedRelationList::last() sometimes returned an ID instead of an item --- src/ORM/UnsavedRelationList.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/ORM/UnsavedRelationList.php b/src/ORM/UnsavedRelationList.php index 4a6a702f296..9e078c63e47 100644 --- a/src/ORM/UnsavedRelationList.php +++ b/src/ORM/UnsavedRelationList.php @@ -257,6 +257,9 @@ public function first() public function last() { $item = end($this->items) ?: null; + 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)]); }