Skip to content

use mongodb driver to check for dirtiness #1664

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions src/Jenssegers/Mongodb/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
use function MongoDB\BSON\fromPHP;
use MongoDB\BSON\ObjectID;
use MongoDB\BSON\UTCDateTime;
use Illuminate\Contracts\Queue\QueueableEntity;
use Illuminate\Contracts\Queue\QueueableCollection;
use MongoDB\Driver\Exception\UnexpectedValueException;

abstract class Model extends BaseModel
{
Expand Down Expand Up @@ -244,20 +246,11 @@ protected function originalIsEquivalent($key, $current)
return false;
}

if ($this->isDateAttribute($key)) {
$current = $current instanceof UTCDateTime ? $this->asDateTime($current) : $current;
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;

return $current == $original;
}

if ($this->hasCast($key)) {
return $this->castAttribute($key, $current) ===
$this->castAttribute($key, $original);
try {
return fromPHP([$current]) === fromPHP([$original]);
} catch (UnexpectedValueException $e) {
return false;
}

return is_numeric($current) && is_numeric($original)
&& strcmp((string) $current, (string) $original) === 0;
}

/**
Expand Down
41 changes: 40 additions & 1 deletion tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,13 +528,52 @@ public function testMultipleLevelDotNotation()
public function testGetDirtyDates()
{
$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()
{
$ids = [
new ObjectId(),
new ObjectId(),
];
$item = new Item([
'numbers' => [1, 2, 3],
'number' => 4,
'ids' => $ids,
'nullable' => 'value',
'fix' => 'fix',
]);
$item->date = $item->fromDateTime(new DateTime('19 august 1989'));
$item->dates = [$item->fromDateTime(new DateTime('19 august 1989'))];
$item->save();

$item = $item->fresh();

$item->numbers = [1, 2, '3'];
$item->nullable = null;
$item->new_val = 'new';
$item->number = '4';
$item->ids = [
new ObjectId((string) $ids[0]),
new ObjectId((string) $ids[1]),
];
$item->date = $item->fromDateTime(new DateTime('19 august 1989'));
$item->dates = [$item->fromDateTime(new DateTime('19 august 1989'))];
$this->assertEquals([
'numbers' => [1, 2, '3'],
'number' => '4',
'nullable' => null,
'new_val' => 'new',
], $item->getDirty());
}

public function testChunkById()
{
User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]);
Expand Down