Skip to content

Commit ee5494b

Browse files
divineGromNaNhalaei
committed
feat!: use mongodb function to check for dirtiness
Co-Authored-By: Jérôme Tamarelle <[email protected]> Co-Authored-By: Hamid Alaei Varnosfaderani <[email protected]>
1 parent e652b0c commit ee5494b

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ All notable changes to this project will be documented in this file.
1919
- 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).
2020
- 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).
2121
- `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).
22+
- 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).
2223

2324
## [3.9.2] - 2022-09-01
2425

src/Eloquent/Model.php

+4-12
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function in_array;
1717
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
1818
use MongoDB\BSON\Binary;
19+
use function MongoDB\BSON\fromPHP;
1920
use MongoDB\BSON\ObjectID;
2021
use MongoDB\BSON\UTCDateTime;
2122
use function uniqid;
@@ -294,20 +295,11 @@ public function originalIsEquivalent($key)
294295
return false;
295296
}
296297

297-
if ($this->isDateAttribute($key)) {
298-
$attribute = $attribute instanceof UTCDateTime ? $this->asDateTime($attribute) : $attribute;
299-
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
300-
301-
return $attribute == $original;
302-
}
303-
304-
if ($this->hasCast($key, static::$primitiveCastTypes)) {
305-
return $this->castAttribute($key, $attribute) ===
306-
$this->castAttribute($key, $original);
298+
if (is_scalar($attribute) || is_scalar($original)) {
299+
return false;
307300
}
308301

309-
return is_numeric($attribute) && is_numeric($original)
310-
&& strcmp((string) $attribute, (string) $original) === 0;
302+
return fromPHP([$attribute]) === fromPHP([$original]);
311303
}
312304

313305
/**

tests/ModelTest.php

+36-1
Original file line numberDiff line numberDiff line change
@@ -876,13 +876,48 @@ public function testMultipleLevelDotNotation(): void
876876
public function testGetDirtyDates(): void
877877
{
878878
$user = new User();
879-
$user->setRawAttributes(['name' => 'John Doe', 'birthday' => new DateTime('19 august 1989')], true);
879+
$user->name = 'John Doe';
880+
$user->birthday = new DateTime('19 august 1989');
881+
$user->syncOriginal();
880882
$this->assertEmpty($user->getDirty());
881883

882884
$user->birthday = new DateTime('19 august 1989');
883885
$this->assertEmpty($user->getDirty());
884886
}
885887

888+
public function testGetDirty()
889+
{
890+
$user = new User([
891+
'name' => 'John Doe',
892+
'email' => '[email protected]',
893+
'phone' => '123456789',
894+
]);
895+
896+
$user->save();
897+
898+
$this->assertFalse($user->isDirty());
899+
900+
$user->phone = '1234555555';
901+
$this->assertTrue($user->isDirty());
902+
903+
$dirty = $user->getDirty();
904+
$this->assertArrayHasKey('phone', $dirty);
905+
$this->assertEquals('1234555555', $dirty['phone']);
906+
907+
$user->email = '[email protected]';
908+
$this->assertTrue($user->isDirty());
909+
$dirty = $user->getDirty();
910+
$this->assertArrayHasKey('phone', $dirty);
911+
$this->assertArrayHasKey('email', $dirty);
912+
$this->assertEquals('1234555555', $dirty['phone']);
913+
$this->assertEquals('[email protected]', $dirty['email']);
914+
915+
$user->save();
916+
917+
$this->assertFalse($user->isDirty());
918+
$this->assertEmpty($user->getDirty());
919+
}
920+
886921
public function testChunkById(): void
887922
{
888923
User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]);

0 commit comments

Comments
 (0)