From a235700469a0ddc59508d905b93fb6777761f2bf Mon Sep 17 00:00:00 2001 From: bstanescu Date: Tue, 3 Jan 2017 13:39:58 +0200 Subject: [PATCH 1/3] Add test for multiple level nested dot notation --- tests/ModelTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 4058f1382..5837229a5 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -495,6 +495,22 @@ public function testDotNotation() $this->assertEquals('Strasbourg', $user['address.city']); } + public function testMultipleLevelDotNotation() + { + $book = Book::create([ + 'title' => 'A Game of Thrones', + 'chapters' => [ + 'one' => [ + 'title' => 'The first chapter', + ], + ], + ]); + + $this->assertEquals(['one' => ['title' => 'The first chapter']], $book->chapters); + $this->assertEquals(['title' => 'The first chapter'], $book['chapters.one']); + $this->assertEquals('The first chapter', $book['chapters.one.title']); + } + public function testGetDirtyDates() { $user = new User(); From 3868446fbb7c11a004f69b5d54121f394f41b26e Mon Sep 17 00:00:00 2001 From: bstanescu Date: Tue, 3 Jan 2017 13:47:24 +0200 Subject: [PATCH 2/3] Fix multiple level attributes using dot notation --- src/Jenssegers/Mongodb/Eloquent/Model.php | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index ce4003dec..772492740 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -237,16 +237,7 @@ public function getAttribute($key) */ protected function getAttributeFromArray($key) { - // Support keys in dot notation. - if (str_contains($key, '.')) { - $attributes = array_dot($this->attributes); - - if (array_key_exists($key, $attributes)) { - return $attributes[$key]; - } - } - - return parent::getAttributeFromArray($key); + return array_get($this->attributes, $key); } /** From d33c040c4df5fa33038bb8625ce46cb489b2cb7b Mon Sep 17 00:00:00 2001 From: bstanescu Date: Thu, 26 Jan 2017 20:34:55 +0200 Subject: [PATCH 3/3] Mantain parent logic on attribute read for non dot keys --- src/Jenssegers/Mongodb/Eloquent/Model.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Jenssegers/Mongodb/Eloquent/Model.php b/src/Jenssegers/Mongodb/Eloquent/Model.php index 772492740..295114ea1 100644 --- a/src/Jenssegers/Mongodb/Eloquent/Model.php +++ b/src/Jenssegers/Mongodb/Eloquent/Model.php @@ -237,7 +237,12 @@ public function getAttribute($key) */ protected function getAttributeFromArray($key) { - return array_get($this->attributes, $key); + // Support keys in dot notation. + if (str_contains($key, '.')) { + return array_get($this->attributes, $key); + } + + return parent::getAttributeFromArray($key); } /**