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] Track model changes after persisting to database #20129

Merged
merged 2 commits into from
Jul 18, 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
55 changes: 55 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ trait HasAttributes
*/
protected $original = [];

/**
* The changed model attributes.
*
* @var array
*/
protected $changes = [];

/**
* The attributes that should be cast to native types.
*
Expand Down Expand Up @@ -924,6 +931,18 @@ public function syncOriginalAttribute($attribute)
return $this;
}

/**
* Sync the changed attributes.
*
* @return $this
*/
public function syncChanges()
{
$this->changes = $this->getDirty();

return $this;
}

/**
* Determine if the model or given attribute(s) have been modified.
*
Expand Down Expand Up @@ -967,6 +986,32 @@ public function isClean($attributes = null)
return ! $this->isDirty(...func_get_args());
}

/**
* Determine if the model or given attribute(s) have been modified.
*
* @param array|string|null $attributes
* @return bool
*/
public function isChanged($attributes = null)
{
$changes = $this->getChanges();

if (is_null($attributes)) {
return count($changes) > 0;
}

$attributes = is_array($attributes)
? $attributes : func_get_args();

foreach ($attributes as $attribute) {
if (array_key_exists($attribute, $changes)) {
return true;
}
}

return false;
}

/**
* Get the attributes that have been changed since last sync.
*
Expand All @@ -985,6 +1030,16 @@ public function getDirty()
return $dirty;
}

/**
* Get the attributes that was changed.
*
* @return array
*/
public function getChanges()
{
return $this->changes;
}

/**
* Determine if the new and old values for a given key are equivalent.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,8 @@ protected function performUpdate(Builder $query)
$this->setKeysForSaveQuery($query)->update($dirty);

$this->fireModelEvent('updated', false);

$this->syncChanges();
}

return true;
Expand Down
47 changes: 43 additions & 4 deletions tests/Integration/Database/EloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,21 @@ public function setUp()
{
parent::setUp();

Schema::create('users', function ($table) {
Schema::create('test_model1', function ($table) {
$table->increments('id');
$table->timestamp('nullable_date')->nullable();
});

Schema::create('test_model2', function ($table) {
$table->increments('id');
$table->string('name');
$table->string('title');
});
}

public function test_user_can_update_nullable_date()
{
$user = EloquentModelTestModel::create([
$user = TestModel1::create([
'nullable_date' => null,
]);

Expand All @@ -48,12 +54,45 @@ public function test_user_can_update_nullable_date()
$user->save();
$this->assertEquals($now->toDateString(), $user->nullable_date->toDateString());
}

public function test_attribute_changes()
{
$user = TestModel2::create([
'name' => str_random(), 'title' => str_random(),
]);

$this->assertEmpty($user->getDirty());
$this->assertEmpty($user->getChanges());
$this->assertFalse($user->isDirty());
$this->assertFalse($user->isChanged());

$user->name = $name = str_random();

$this->assertEquals(['name' => $name], $user->getDirty());
$this->assertEmpty($user->getChanges());
$this->assertTrue($user->isDirty());
$this->assertFalse($user->isChanged());

$user->save();

$this->assertEmpty($user->getDirty());
$this->assertEquals(['name' => $name], $user->getChanges());
$this->assertTrue($user->isChanged());
$this->assertTrue($user->isChanged('name'));
}
}

class EloquentModelTestModel extends Model
class TestModel1 extends Model
{
public $table = 'users';
public $table = 'test_model1';
public $timestamps = false;
protected $guarded = ['id'];
protected $dates = ['nullable_date'];
}

class TestModel2 extends Model
{
public $table = 'test_model2';
public $timestamps = false;
protected $guarded = ['id'];
}