Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 71b7861

Browse files
divinehalaei
andcommitted
feat!: use mongodb function to check for dirtiness
Co-Authored-By: Hamid Alaei Varnosfaderani <hamid.a85@gmail.com>
1 parent 57010c0 commit 71b7861

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"illuminate/container": "^10.0",
2424
"illuminate/database": "^10.0",
2525
"illuminate/events": "^10.0",
26-
"mongodb/mongodb": "^1.15"
26+
"mongodb/mongodb": "^1.15",
27+
"ext-mongodb": "*"
2728
},
2829
"require-dev": {
2930
"phpunit/phpunit": "^9.5.10",

‎src/Eloquent/Model.php‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use MongoDB\BSON\Binary;
1919
use MongoDB\BSON\ObjectID;
2020
use MongoDB\BSON\UTCDateTime;
21+
use function MongoDB\BSON\fromPHP;
22+
use MongoDB\Driver\Exception\UnexpectedValueException;
2123
use function uniqid;
2224

2325
abstract class Model extends BaseModel
@@ -259,20 +261,11 @@ public function originalIsEquivalent($key)
259261
return false;
260262
}
261263

262-
if ($this->isDateAttribute($key)) {
263-
$attribute = $attribute instanceof UTCDateTime ? $this->asDateTime($attribute) : $attribute;
264-
$original = $original instanceof UTCDateTime ? $this->asDateTime($original) : $original;
265-
266-
return $attribute == $original;
267-
}
268-
269-
if ($this->hasCast($key, static::$primitiveCastTypes)) {
270-
return $this->castAttribute($key, $attribute) ===
271-
$this->castAttribute($key, $original);
264+
try {
265+
return (fromPHP([$attribute]) === fromPHP([$original]));
266+
} catch (UnexpectedValueException $e) {
267+
return false;
272268
}
273-
274-
return is_numeric($attribute) && is_numeric($original)
275-
&& strcmp((string) $attribute, (string) $original) === 0;
276269
}
277270

278271
/**

‎tests/ModelTest.php‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,13 +715,48 @@ public function testMultipleLevelDotNotation(): void
715715
public function testGetDirtyDates(): void
716716
{
717717
$user = new User();
718-
$user->setRawAttributes(['name' => 'John Doe', 'birthday' => new DateTime('19 august 1989')], true);
718+
$user->name = 'John Doe';
719+
$user->birthday = new DateTime('19 august 1989');
720+
$user->syncOriginal();
719721
$this->assertEmpty($user->getDirty());
720722

721723
$user->birthday = new DateTime('19 august 1989');
722724
$this->assertEmpty($user->getDirty());
723725
}
724726

727+
public function testGetDirty()
728+
{
729+
$user = new User([
730+
'name' => 'John Doe',
731+
'email' => 'john.doe@example.com',
732+
'phone' => '123456789',
733+
]);
734+
735+
$user->save();
736+
737+
$this->assertFalse($user->isDirty());
738+
739+
$user->phone = '1234555555';
740+
$this->assertTrue($user->isDirty());
741+
742+
$dirty = $user->getDirty();
743+
$this->assertArrayHasKey('phone', $dirty);
744+
$this->assertEquals('1234555555', $dirty['phone']);
745+
746+
$user->email = 'jane.doe@example.com';
747+
$this->assertTrue($user->isDirty());
748+
$dirty = $user->getDirty();
749+
$this->assertArrayHasKey('phone', $dirty);
750+
$this->assertArrayHasKey('email', $dirty);
751+
$this->assertEquals('1234555555', $dirty['phone']);
752+
$this->assertEquals('jane.doe@example.com', $dirty['email']);
753+
754+
$user->save();
755+
756+
$this->assertFalse($user->isDirty());
757+
$this->assertEmpty($user->getDirty());
758+
}
759+
725760
public function testChunkById(): void
726761
{
727762
User::create(['name' => 'fork', 'tags' => ['sharp', 'pointy']]);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /