-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat!: use mongodb function to check for dirtiness #2515
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
base: 5.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A couple of notes on this:
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. |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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']]); | ||
|
Uh oh!
There was an error while loading. Please reload this page.