Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: UnsavedRelationList first/last to return null if list is empty (fixes #11083) #11085

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/ORM/UnsavedRelationList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)])) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not actually part of this fix but seemed like sensible defensive coding. Happy to remove or break out into a separate commit if preferred

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems sensible.

$item->update($this->extraFields[key($this->items)]);
}
return $item;
Expand All @@ -256,8 +256,11 @@ public function first()
*/
public function last()
{
$item = end($this->items);
if (!empty($this->extraFields[key($this->items)])) {
$item = end($this->items) ?: null;
if (is_numeric($item)) {
$item = DataObject::get_by_id($this->dataClass, $item);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pushed a second commit for this bugfix. tldr; UnsavedRelationList items array can contain both full objects (for unsaved records) or just IDs (for already existing records). This code already exists in first(), I think last() was just overlooked.

Covered by the test already added for the main fix (that’s how I noticed this!): https://github.com/silverstripe/silverstripe-framework/actions/runs/7085966036/job/19283271493?pr=11085

}
if ($item && !empty($this->extraFields[key($this->items)])) {
$item->update($this->extraFields[key($this->items)]);
}
return $item;
Expand Down
16 changes: 16 additions & 0 deletions tests/php/ORM/UnsavedRelationListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.');
}
}