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.3] Fix regression in save(touch) option #15264

Merged
merged 1 commit into from
Sep 5, 2016
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
7 changes: 4 additions & 3 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1462,7 +1462,7 @@ public function save(array $options = [])
// clause to only update this model. Otherwise, we'll just insert them.
if ($this->exists) {
$saved = $this->isDirty() ?
$this->performUpdate($query) : true;
$this->performUpdate($query, $options) : true;
}

// If the model is brand new, we'll insert it into our database and set the
Expand Down Expand Up @@ -1515,9 +1515,10 @@ protected function finishSave(array $options)
* Perform a model update operation.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $options
* @return bool
*/
protected function performUpdate(Builder $query)
protected function performUpdate(Builder $query, array $options = [])
{
// If the updating event returns false, we will cancel the update operation so
// developers can hook Validation systems into their models and cancel this
Expand All @@ -1529,7 +1530,7 @@ protected function performUpdate(Builder $query)
// First we need to create a fresh query instance and touch the creation and
// update timestamp on the model which are maintained by us for developer
// convenience. Then we will just continue saving the model instances.
if ($this->timestamps) {
if ($this->timestamps && Arr::get($options, 'touch', true)) {
$this->updateTimestamps();
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,23 @@ public function testUpdateProcessDoesntOverrideTimestamps()
$this->assertTrue($model->save());
}

public function testSaveDoesntUpateTimestampsIfTouchOptionDisabled()
{
$model = $this->getMockBuilder('EloquentModelStub')->setMethods(['newQueryWithoutScopes', 'updateTimestamps', 'fireModelEvent'])->getMock();
$query = m::mock('Illuminate\Database\Eloquent\Builder');
$query->shouldReceive('where')->once()->with('id', '=', 1);
$query->shouldReceive('update')->once()->with(['name' => 'taylor'])->andReturn(1);
$model->expects($this->once())->method('newQueryWithoutScopes')->will($this->returnValue($query));
$model->expects($this->never())->method('updateTimestamps');
$model->expects($this->any())->method('fireModelEvent')->will($this->returnValue(true));

$model->id = 1;
$model->syncOriginal();
$model->name = 'taylor';
$model->exists = true;
$this->assertTrue($model->save(['touch' => false]));
}

public function testSaveIsCancelledIfSavingEventReturnsFalse()
{
$model = $this->getMockBuilder('EloquentModelStub')->setMethods(['newQueryWithoutScopes'])->getMock();
Expand Down