Skip to content

fix(database): prevent deeply nested has many map adding extra empty object#1995

Open
nunntom wants to merge 2 commits intotempestphp:3.xfrom
nunntom:deeply-nested-has-many-map-bug
Open

fix(database): prevent deeply nested has many map adding extra empty object#1995
nunntom wants to merge 2 commits intotempestphp:3.xfrom
nunntom:deeply-nested-has-many-map-bug

Conversation

@nunntom
Copy link

@nunntom nunntom commented Feb 19, 2026

See #1994

  • Added failing assertion to existing test showing the bug
  • Removed line in SelectModelMapper that adds initial empty model. It doesn't seem necessary since the later $data->set adds the full nested path anyway. Please correct me if I'm wrong or if I broke anything. Tests are all still passing (including the failing one I added).

}

if ($key) {
$data->set($key, $value);
Copy link
Author

Choose a reason for hiding this comment

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

This $data-set seems like it adds the whole recursive path to this key, making the earlier $data->set with $originalKey unnecessary.

$originalKey .= $relation->name . '.';

if (! $data->hasKey(trim($originalKey, '.'))) {
$data->set(trim($originalKey, '.'), []);
Copy link
Author

Choose a reason for hiding this comment

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

When about to add $key: "books.1.chapters.1.id", $originalKey is "books.chapters." so this adds an associative "chapters" => [] into the books array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

Comments