Skip to content

Commit

Permalink
Fix getDirty check for dates, fixes mongodb#373
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Dec 22, 2015
1 parent 712c88a commit a2f4eca
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/Jenssegers/Mongodb/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,29 @@ public function attributesToArray()
return $attributes;
}

/**
* Determine if the new and old values for a given key are numerically equivalent.
*
* @param string $key
* @return bool
*/
protected function originalIsNumericallyEquivalent($key)
{
$current = $this->attributes[$key];

$original = $this->original[$key];

// Date comparison.
if (in_array($key, $this->getDates()))
{
$current = $current instanceof MongoDate ? $this->asDateTime($current) : $current;
$original = $original instanceof MongoDate ? $this->asDateTime($original) : $original;
return $current == $original;
}

return parent::originalIsNumericallyEquivalent($key);
}

/**
* Remove one or more fields.
*
Expand Down
10 changes: 10 additions & 0 deletions tests/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,4 +490,14 @@ public function testDotNotation()
$this->assertEquals('Paris', $user->{'address.city'});
}

public function testGetDirtyDates()
{
$user = new User();
$user->setRawAttributes(['name' => 'John Doe', 'birthday' => new DateTime('19 august 1989')], true);
$this->assertEmpty($user->getDirty());

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

}

0 comments on commit a2f4eca

Please sign in to comment.