Skip to content
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

[5.5] add date and cast check for dirty #18400

Merged
merged 8 commits into from
Apr 6, 2017
Merged
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
36 changes: 26 additions & 10 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,11 +960,8 @@ public function getDirty()
{
$dirty = [];

foreach ($this->attributes as $key => $value) {
if (! array_key_exists($key, $this->original)) {
$dirty[$key] = $value;
} elseif ($value !== $this->original[$key] &&
! $this->originalIsNumericallyEquivalent($key)) {
foreach ($this->getAttributes() as $key => $value) {
if (! $this->originalIsEquivalent($key, $value)) {
$dirty[$key] = $value;
}
}
Expand All @@ -973,16 +970,35 @@ public function getDirty()
}

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

$original = $this->original[$key];
if ($current === $original = $this->getOriginal($key)) {
return true;
}

// When check rich this check and current attribute value not equals with original, we should skip next steps
// if current is null
if (is_null($current)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check for nullable may be should move to fromDateTime method as like castAttribute method.

This hack check looked from setAttribute

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully agree, either that or should to include the original value in the check too:

 if (is_null($current) || is_null($original)) {
  • is_null($current) covers updating filled column with NULL
  • is_null($original) covers updating NULL with filling

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The missing is_null($original) causes problems for deleted_at which is NULL before a model is updated.

return false;
}

if ($this->isDateAttribute($key)) {
return $this->fromDateTime($current) === $this->fromDateTime($original);
}

if ($this->hasCast($key)) {
return $this->castAttribute($key, $current) === $this->castAttribute($key, $original);
}

// This method checks if the two values are numerically equivalent even if they
// are different types. This is in case the two values are not the same type
Expand Down
25 changes: 25 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ public function testDirtyAttributes()
$this->assertTrue($model->isDirty(['foo', 'bar']));
}

public function testDirtyOnCastOrDateAttributes()
{
$model = new EloquentModelCastingStub;
$model->setDateFormat('Y-m-d H:i:s');
$model->boolAttribute = 1;
$model->foo = 1;
$model->bar = '2017-03-18';
$model->dateAttribute = '2017-03-18';
$model->datetimeAttribute = '2017-03-23 22:17:00';
$model->syncOriginal();

$model->boolAttribute = true;
$model->foo = true;
$model->bar = '2017-03-18 00:00:00';
$model->dateAttribute = '2017-03-18 00:00:00';
$model->datetimeAttribute = null;

$this->assertTrue($model->isDirty());
$this->assertTrue($model->isDirty('foo'));
$this->assertTrue($model->isDirty('bar'));
$this->assertFalse($model->isDirty('boolAttribute'));
$this->assertFalse($model->isDirty('dateAttribute'));
$this->assertTrue($model->isDirty('datetimeAttribute'));
}

public function testCleanAttributes()
{
$model = new EloquentModelStub(['foo' => '1', 'bar' => 2, 'baz' => 3]);
Expand Down