Skip to content

Commit

Permalink
feat!: use mongodb function to check for dirtiness
Browse files Browse the repository at this point in the history
Co-Authored-By: Jérôme Tamarelle <GromNaN@users.noreply.github.com>
Co-Authored-By: Hamid Alaei Varnosfaderani <hamid.a85@gmail.com>
  • Loading branch information
3 people committed Aug 26, 2023
1 parent e652b0c commit ee5494b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 4 additions & 12 deletions src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}

/**
Expand Down
37 changes: 36 additions & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']]);
Expand Down

0 comments on commit ee5494b

Please sign in to comment.