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

[11.x] Add "previous" state to models #53901

Open
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Open
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
45 changes: 45 additions & 0 deletions src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ trait HasAttributes
*/
protected $original = [];

/**
* The model attribute's previous state.
*
* @var array
*/
protected $previous = [];

/**
* The changed model attributes.
*
Expand Down Expand Up @@ -1959,6 +1966,32 @@ public function getRawOriginal($key = null, $default = null)
return Arr::get($this->original, $key, $default);
}

/**
* Get the model's previous attribute values.
*
* @param string|null $key
* @param mixed $default
* @return mixed|array
*/
public function getPrevious($key = null, $default = null)
{
return (new static)->setRawAttributes(
$this->previous, $sync = true
)->getOriginalWithoutRewindingModel($key, $default);
}

/**
* Get the model's raw previous attribute values.
*
* @param string|null $key
* @param mixed $default
* @return mixed|array
*/
public function getRawPrevious($key = null, $default = null)
{
return Arr::get($this->previous, $key, $default);
}

/**
* Get a subset of the model's attributes.
*
Expand Down Expand Up @@ -2018,6 +2051,18 @@ public function syncOriginalAttributes($attributes)
return $this;
}

/**
* Sync the previous attributes with the original.
*
* @return $this
*/
public function syncPrevious()
{
$this->previous = $this->original;

return $this;
}

/**
* Sync the changed attributes.
*
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ protected function incrementOrDecrement($column, $amount, $extra, $method)
$amount = (clone $this)->setAttribute($column, $amount)->getAttributeFromArray($column);
}

$this->syncPrevious();

return tap($this->setKeysForSaveQuery($this->newQueryWithoutScopes())->{$method}($column, $amount, $extra), function () use ($column) {
$this->syncChanges();

Expand Down Expand Up @@ -1236,6 +1238,8 @@ protected function performUpdate(Builder $query)
$dirty = $this->getDirtyForUpdate();

if (count($dirty) > 0) {
$this->syncPrevious();

$this->setKeysForSaveQuery($query)->update($dirty);

$this->syncChanges();
Expand Down
9 changes: 9 additions & 0 deletions tests/Integration/Database/EloquentDeleteTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ protected function afterRefreshingDatabase()
});
}

public function testBasicDelete()
{
$post = Post::create();

Post::where('id', $post->id)->delete();

$this->assertCount(0, Post::all());
}

public function testDeleteWithLimit()
{
if ($this->driver === 'sqlsrv') {
Expand Down
12 changes: 12 additions & 0 deletions tests/Integration/Database/EloquentModelRefreshTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,18 @@ public function testItSyncsOriginalOnRefresh()
$this->assertSame('patrick', $post->getOriginal('title'));
}

public function testItDoesNotSyncPreviousOnRefresh()
{
$post = Post::create(['title' => 'pat']);

Post::find($post->id)->update(['title' => 'patrick']);

$post->refresh();

$this->assertEmpty($post->getDirty());
$this->assertEmpty($post->getPrevious());
}

public function testAsPivot()
{
Schema::create('post_posts', function (Blueprint $table) {
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/Database/EloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ public function testDiscardChanges()
$this->assertFalse($user->wasChanged());
$this->assertSame($originalName, $user->getOriginal('name'));
$this->assertSame($overrideName, $user->getAttribute('name'));
$this->assertEmpty($user->getPrevious());

$user->discardChanges();

$this->assertEmpty($user->getDirty());
$this->assertSame($originalName, $user->getOriginal('name'));
$this->assertSame($originalName, $user->getAttribute('name'));
$this->assertEmpty($user->getPrevious());

$user->save();
$this->assertFalse($user->wasChanged());
Expand Down
45 changes: 43 additions & 2 deletions tests/Integration/Database/EloquentUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function testBasicUpdate()
'title' => 'Ms.',
]);

TestUpdateModel1::where('title', 'Ms.')->delete();
TestUpdateModel1::where('title', 'Ms.')->update(['title' => 'Dr.']);

$this->assertCount(0, TestUpdateModel1::all());
$this->assertSame('Dr.', TestUpdateModel1::first()->title);
}

public function testUpdateWithLimitsAndOrders()
Expand Down Expand Up @@ -136,6 +136,47 @@ public function testIncrementOrDecrementIgnoresGlobalScopes()
$deletedModel->decrement('counter');
$this->assertEquals(0, $deletedModel->fresh()->counter);
}

public function testUpdateSyncsPrevious()
{
$model = TestUpdateModel1::create([
'name' => Str::random(),
'title' => 'Ms.',
]);

$model->update(['title' => 'Dr.']);

$this->assertSame('Dr.', $model->title);
$this->assertSame('Dr.', $model->getOriginal('title'));
$this->assertSame('Ms.', $model->getPrevious('title'));
}

public function testSaveSyncsPrevious()
{
$model = TestUpdateModel1::create([
'name' => Str::random(),
'title' => 'Ms.',
]);

$model->title = 'Dr.';
$model->save();

$this->assertSame('Dr.', $model->title);
$this->assertSame('Dr.', $model->getOriginal('title'));
$this->assertSame('Ms.', $model->getPrevious('title'));
}

public function testIncrementSyncsPrevious()
{
$model = TestUpdateModel3::create([
'counter' => 0,
]);

$model->increment('counter');

$this->assertEquals(1, $model->counter);
$this->assertEquals(0, $model->getPrevious('counter'));
}
}

class TestUpdateModel1 extends Model
Expand Down