From 1a9095f6e8a89c5dcebf9c45f62962af5445a00e Mon Sep 17 00:00:00 2001 From: Maxime Fabre Date: Sun, 4 Sep 2016 10:40:46 +0200 Subject: [PATCH] Fix regression in save(touch) option --- src/Illuminate/Database/Eloquent/Model.php | 7 ++++--- tests/Database/DatabaseEloquentModelTest.php | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/Illuminate/Database/Eloquent/Model.php b/src/Illuminate/Database/Eloquent/Model.php index ed57b5d76b15..197f510734f2 100644 --- a/src/Illuminate/Database/Eloquent/Model.php +++ b/src/Illuminate/Database/Eloquent/Model.php @@ -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 @@ -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 @@ -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(); } diff --git a/tests/Database/DatabaseEloquentModelTest.php b/tests/Database/DatabaseEloquentModelTest.php index fc5cd375e895..45ec60ae2929 100755 --- a/tests/Database/DatabaseEloquentModelTest.php +++ b/tests/Database/DatabaseEloquentModelTest.php @@ -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();