From 08b7f4afde61bf5eb181bc82ffe92b952b485282 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 21 May 2024 17:46:01 +0200 Subject: [PATCH] PHPORM-184 Use fixed key for temporary setting nested field --- CHANGELOG.md | 6 +++++- src/Eloquent/Model.php | 11 +++++------ 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ffd240b43..9eff79c84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,11 @@ # Changelog All notable changes to this project will be documented in this file. -## [4.3.0] - unreleased +## [4.3.1] + +* Fix memory leak when filling nested fields using dot notation by @GromNaN in [#2962](https://github.com/mongodb/laravel-mongodb/pull/2962) + +## [4.3.0] - 2024-04-26 * New aggregation pipeline builder by @GromNaN in [#2738](https://github.com/mongodb/laravel-mongodb/pull/2738) * Drop support for Composer 1.x by @GromNaN in [#2784](https://github.com/mongodb/laravel-mongodb/pull/2784) diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index de5ddc3ea..5974e49e1 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -46,7 +46,6 @@ use function str_contains; use function str_starts_with; use function strcmp; -use function uniqid; use function var_export; /** @mixin Builder */ @@ -55,6 +54,8 @@ abstract class Model extends BaseModel use HybridRelations; use EmbedsRelations; + private const TEMPORARY_KEY = '__LARAVEL_TEMPORARY_KEY__'; + /** * The collection associated with the model. * @@ -271,12 +272,10 @@ public function setAttribute($key, $value) // Support keys in dot notation. if (str_contains($key, '.')) { // Store to a temporary key, then move data to the actual key - $uniqueKey = uniqid($key); - - parent::setAttribute($uniqueKey, $value); + parent::setAttribute(self::TEMPORARY_KEY, $value); - Arr::set($this->attributes, $key, $this->attributes[$uniqueKey] ?? null); - unset($this->attributes[$uniqueKey]); + Arr::set($this->attributes, $key, $this->attributes[self::TEMPORARY_KEY] ?? null); + unset($this->attributes[self::TEMPORARY_KEY]); return $this; }