From ee5494b5fc12ba4540fbb303000ea49967f99440 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Sat, 26 Aug 2023 07:41:30 +0000 Subject: [PATCH 1/2] feat!: use mongodb function to check for dirtiness MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Jérôme Tamarelle Co-Authored-By: Hamid Alaei Varnosfaderani --- CHANGELOG.md | 1 + src/Eloquent/Model.php | 16 ++++------------ tests/ModelTest.php | 37 ++++++++++++++++++++++++++++++++++++- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dbdee597..5e9b228bd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file. - Change signature of `Query\Builder::__constructor` to match the parent class [#26](https://github.com/GromNaN/laravel-mongodb-private/pull/26) by [@GromNaN](https://github.com/GromNaN). - Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear`, `whereTime` to use MongoDB operators [#2570](https://github.com/jenssegers/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu) and [@GromNaN](https://github.com/GromNaN). - `Model::unset()` does not persist the change. Call `Model::save()` to persist the change [#2578](https://github.com/jenssegers/laravel-mongodb/pull/2578) by [@GromNaN](https://github.com/GromNaN). +- Use mongodb function to check for dirtiness [#2515](https://github.com/jenssegers/laravel-mongodb/pull/2515) by [@halaei](https://github.com/halaei) and [@GromNaN](https://github.com/GromNaN). ## [3.9.2] - 2022-09-01 diff --git a/src/Eloquent/Model.php b/src/Eloquent/Model.php index 4e118c46f..e8685cf53 100644 --- a/src/Eloquent/Model.php +++ b/src/Eloquent/Model.php @@ -16,6 +16,7 @@ use function in_array; use Jenssegers\Mongodb\Query\Builder as QueryBuilder; use MongoDB\BSON\Binary; +use function MongoDB\BSON\fromPHP; use MongoDB\BSON\ObjectID; use MongoDB\BSON\UTCDateTime; use function uniqid; @@ -294,20 +295,11 @@ public function originalIsEquivalent($key) return false; } - if ($this->isDateAttribute($key)) { - $attribute = $attribute instanceof UTCDateTime ? $this->asDateTime($attribute) : $attribute; - $original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original; - - return $attribute == $original; - } - - if ($this->hasCast($key, static::$primitiveCastTypes)) { - return $this->castAttribute($key, $attribute) === - $this->castAttribute($key, $original); + if (is_scalar($attribute) || is_scalar($original)) { + return false; } - return is_numeric($attribute) && is_numeric($original) - && strcmp((string) $attribute, (string) $original) === 0; + return fromPHP([$attribute]) === fromPHP([$original]); } /** diff --git a/tests/ModelTest.php b/tests/ModelTest.php index 75dfaf4bf..bdc50e808 100644 --- a/tests/ModelTest.php +++ b/tests/ModelTest.php @@ -876,13 +876,48 @@ public function testMultipleLevelDotNotation(): void public function testGetDirtyDates(): void { $user = new User(); - $user->setRawAttributes(['name' => 'John Doe', 'birthday' => new DateTime('19 august 1989')], true); + $user->name = 'John Doe'; + $user->birthday = new DateTime('19 august 1989'); + $user->syncOriginal(); $this->assertEmpty($user->getDirty()); $user->birthday = new DateTime('19 august 1989'); $this->assertEmpty($user->getDirty()); } + public function testGetDirty() + { + $user = new User([ + 'name' => 'John Doe', + 'email' => 'john.doe@example.com', + 'phone' => '123456789', + ]); + + $user->save(); + + $this->assertFalse($user->isDirty()); + + $user->phone = '1234555555'; + $this->assertTrue($user->isDirty()); + + $dirty = $user->getDirty(); + $this->assertArrayHasKey('phone', $dirty); + $this->assertEquals('1234555555', $dirty['phone']); + + $user->email = 'jane.doe@example.com'; + $this->assertTrue($user->isDirty()); + $dirty = $user->getDirty(); + $this->assertArrayHasKey('phone', $dirty); + $this->assertArrayHasKey('email', $dirty); + $this->assertEquals('1234555555', $dirty['phone']); + $this->assertEquals('jane.doe@example.com', $dirty['email']); + + $user->save(); + + $this->assertFalse($user->isDirty()); + $this->assertEmpty($user->getDirty()); + } + public function testChunkById(): void { User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]); From 2a2bb47cf5f24918f6ae9eaf6d6766a9afc45da2 Mon Sep 17 00:00:00 2001 From: divine <48183131+divine@users.noreply.github.com> Date: Sun, 27 Aug 2023 12:02:58 +0000 Subject: [PATCH 2/2] chore: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e9b228bd..918d05ca2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ All notable changes to this project will be documented in this file. - Change signature of `Query\Builder::__constructor` to match the parent class [#26](https://github.com/GromNaN/laravel-mongodb-private/pull/26) by [@GromNaN](https://github.com/GromNaN). - Fix Query on `whereDate`, `whereDay`, `whereMonth`, `whereYear`, `whereTime` to use MongoDB operators [#2570](https://github.com/jenssegers/laravel-mongodb/pull/2376) by [@Davpyu](https://github.com/Davpyu) and [@GromNaN](https://github.com/GromNaN). - `Model::unset()` does not persist the change. Call `Model::save()` to persist the change [#2578](https://github.com/jenssegers/laravel-mongodb/pull/2578) by [@GromNaN](https://github.com/GromNaN). -- Use mongodb function to check for dirtiness [#2515](https://github.com/jenssegers/laravel-mongodb/pull/2515) by [@halaei](https://github.com/halaei) and [@GromNaN](https://github.com/GromNaN). +- Use `MongoDB\BSON\fromPHP` to detect changes [#2515](https://github.com/jenssegers/laravel-mongodb/pull/2515) by [@halaei](https://github.com/halaei) and [@divine](https://github.com/divine). ## [3.9.2] - 2022-09-01