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 $attributesToReloadOnSave property for Eloquent model #51014

Draft
wants to merge 5 commits into
base: 11.x
Choose a base branch
from
Draft
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
19 changes: 18 additions & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ abstract class Model implements Arrayable, ArrayAccess, CanBeEscapedWhenCastToSt
*/
protected $withCount = [];

/**
* The attributes to reload on save.
*
* @var array<int, string>
*/
protected $attributesToReloadOnSave = [];
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if it's too generic but maybe just $reload?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I wasn't sure about naming too, $reload feels like reloading relations? specially when we have load method on eloquent.

Copy link
Member

Choose a reason for hiding this comment

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

That's true.


/**
* Indicates whether lazy loading will be prevented on this model.
*
Expand Down Expand Up @@ -1178,6 +1185,16 @@ public function saveOrFail(array $options = [])
*/
protected function finishSave(array $options)
{
if (! empty($this->attributesToReloadOnSave)) {
$this->setRawAttributes(array_merge(
$this->attributes,
$this->setKeysForSelectQuery($this->newQueryWithoutScopes())
->useWritePdo()
->firstOrFail($this->attributesToReloadOnSave)
->attributes
));
}

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

if ($this->isDirty() && ($options['touch'] ?? true)) {
Expand Down Expand Up @@ -1689,7 +1706,7 @@ public function fresh($with = [])
}

/**
* Reload the current model instance with fresh attributes from the database.
* Reload the current model instance with fresh attributes and relationships from the database.
*
* @return $this
*/
Expand Down
78 changes: 78 additions & 0 deletions tests/Integration/Database/EloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,76 @@ public function testInsertRecordWithReservedWordFieldName()
'analyze' => true,
]);
}

public function testHasGeneratedColumns()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->integer('price');
$table->boolean('in_stock')->default(true);

if ($this->driver === 'sqlsrv') {
$table->computed('tax', 'price * 0.6');
$table->computed('total', 'price * 1.6')->persisted();
} else {
if ($this->driver === 'pgsql') {
$table->integer('tax')->storedAs('price * 0.6');
} else {
$table->integer('tax')->virtualAs('price * 0.6');
}
$table->integer('total')->storedAs('price * 1.6');
}
});

Product::retrieved(function (Model $product) {
$this->assertEquals([
'tax' => 12,
'total' => 32,
'in_stock' => 1,
], $product->getAttributes());
});
Product::saved(function ($product) {
$this->assertEquals(20, $product->price);
$this->assertEquals(12, $product->tax);
$this->assertEquals(32, $product->total);
$this->assertTrue($product->in_stock);
});

$instance = Product::create(['price' => 20]);

$this->assertEquals(20, $instance->price);
$this->assertEquals(12, $instance->tax);
$this->assertEquals(32, $instance->total);
$this->assertTrue($instance->in_stock);
$this->assertFalse($instance->isDirty());

Product::flushEventListeners();
Product::retrieved(function ($product) {
$this->assertEquals([
'tax' => 6,
'total' => 16,
'in_stock' => 1,
], $product->getAttributes());
});
Product::saved(function ($product) {
$this->assertTrue($product->isDirty('price'));
$this->assertTrue($product->isDirty('tax'));
$this->assertTrue($product->isDirty('total'));
$this->assertFalse($product->isDirty('in_stock'));
$this->assertEquals(10, $product->price);
$this->assertEquals(6, $product->tax);
$this->assertEquals(16, $product->total);
$this->assertTrue($product->in_stock);
});

$instance->update(['price' => 10]);

$this->assertEquals(10, $instance->price);
$this->assertEquals(6, $instance->tax);
$this->assertEquals(16, $instance->total);
$this->assertTrue($instance->in_stock);
$this->assertFalse($instance->isDirty());
}
}

class TestModel1 extends Model
Expand All @@ -142,3 +212,11 @@ class TestModel2 extends Model
public $timestamps = false;
protected $guarded = [];
}

class Product extends Model
{
protected $fillable = ['price', 'in_stock'];
protected $casts = ['in_stock' => 'boolean'];
public $timestamps = false;
protected $attributesToReloadOnSave = ['tax', 'total', 'in_stock'];
}