Skip to content

Commit

Permalink
Fix regression in save(touch) option (#15264)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anahkiasen authored and taylorotwell committed Sep 5, 2016
1 parent eeae0bb commit 6c3ecdc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
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

0 comments on commit 6c3ecdc

Please sign in to comment.