Skip to content

Commit a2f4eca

Browse files
committed
Fix getDirty check for dates, fixes #373
1 parent 712c88a commit a2f4eca

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,29 @@ public function attributesToArray()
355355
return $attributes;
356356
}
357357

358+
/**
359+
* Determine if the new and old values for a given key are numerically equivalent.
360+
*
361+
* @param string $key
362+
* @return bool
363+
*/
364+
protected function originalIsNumericallyEquivalent($key)
365+
{
366+
$current = $this->attributes[$key];
367+
368+
$original = $this->original[$key];
369+
370+
// Date comparison.
371+
if (in_array($key, $this->getDates()))
372+
{
373+
$current = $current instanceof MongoDate ? $this->asDateTime($current) : $current;
374+
$original = $original instanceof MongoDate ? $this->asDateTime($original) : $original;
375+
return $current == $original;
376+
}
377+
378+
return parent::originalIsNumericallyEquivalent($key);
379+
}
380+
358381
/**
359382
* Remove one or more fields.
360383
*

tests/ModelTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,4 +490,14 @@ public function testDotNotation()
490490
$this->assertEquals('Paris', $user->{'address.city'});
491491
}
492492

493+
public function testGetDirtyDates()
494+
{
495+
$user = new User();
496+
$user->setRawAttributes(['name' => 'John Doe', 'birthday' => new DateTime('19 august 1989')], true);
497+
$this->assertEmpty($user->getDirty());
498+
499+
$user->birthday = new DateTime('19 august 1989');
500+
$this->assertEmpty($user->getDirty());
501+
}
502+
493503
}

0 commit comments

Comments
 (0)