Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: use mongodb function to check for dirtiness #2515

Open
wants to merge 2 commits into
base: 4.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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).
divine marked this conversation as resolved.
Show resolved Hide resolved

## [3.9.2] - 2022-09-01

Expand Down
16 changes: 4 additions & 12 deletions src/Eloquent/Model.php
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;
}

divine marked this conversation as resolved.
Show resolved Hide resolved
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]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of notes on this:

  • fromPHP will be deprecated and is replaced by Document::fromPHP
  • Root-Level encoding is only supported for documents (i.e. any object or array with string keys)
  • If $attribute happens to be an object (e.g. an embedded document), this conversion will not yield the correct results as only public properties are encoded, which is most likely wrong for any Model
  • This may not be an issue, but I'll note that using this comparison ['foo' => 'bar', 'bar' => 'foo'] and ['bar' => 'foo', 'foo' => 'bar'] will not be treated as equal. That said, we don't have a good way to check BSON equality, and one may argue that two BSON documents with the same key/value pairs in different order shouldn't be considered equal.

For the time being, using this comparison may be feasible as long as we can ensure that it doesn't have side effects for embedded documents.

}

/**
Expand Down
37 changes: 36 additions & 1 deletion tests/ModelTest.php
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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test using embedded documents and arrays to ensure correct behaviour.

{
$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