Skip to content

Commit

Permalink
Merge pull request #27277 from staudenmeir/26158
Browse files Browse the repository at this point in the history
[5.7] Revert and fix #26158
  • Loading branch information
taylorotwell authored Jan 23, 2019
2 parents 9d232c8 + d78ca49 commit c3f3022
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ protected function decrement($column, $amount = 1, array $extra = [])
*/
protected function incrementOrDecrement($column, $amount, $extra, $method)
{
$query = $this->newModelQuery();
$query = $this->newQueryWithoutRelationships();

if (! $this->exists) {
return $query->{$method}($column, $amount, $extra);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -821,7 +821,7 @@ public function touch()
// the related model's timestamps, to make sure these all reflect the changes
// to the parent models. This will help us keep any caching synced up here.
if (count($ids = $this->allRelatedIds()) > 0) {
$this->getRelated()->newModelQuery()->whereIn($key, $ids)->update($columns);
$this->getRelated()->newQueryWithoutRelationships()->whereIn($key, $ids)->update($columns);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public function delete()
*/
protected function getDeleteQuery()
{
return $this->newModelQuery()->where([
return $this->newQueryWithoutRelationships()->where([
$this->foreignKey => $this->getOriginal($this->foreignKey, $this->getAttribute($this->foreignKey)),
$this->relatedKey => $this->getOriginal($this->relatedKey, $this->getAttribute($this->relatedKey)),
]);
Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1446,13 +1446,13 @@ public function testReplicateCreatesANewModelInstanceWithSameAttributeValues()

public function testIncrementOnExistingModelCallsQueryAndSetsAttribute()
{
$model = m::mock(EloquentModelStub::class.'[newModelQuery]');
$model = m::mock(EloquentModelStub::class.'[newQueryWithoutRelationships]');
$model->exists = true;
$model->id = 1;
$model->syncOriginalAttribute('id');
$model->foo = 2;

$model->shouldReceive('newModelQuery')->andReturn($query = m::mock(stdClass::class));
$model->shouldReceive('newQueryWithoutRelationships')->andReturn($query = m::mock(stdClass::class));
$query->shouldReceive('where')->andReturn($query);
$query->shouldReceive('increment');

Expand Down
4 changes: 2 additions & 2 deletions tests/Database/DatabaseEloquentPivotTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ public function testKeysCanBeSetProperly()

public function testDeleteMethodDeletesModelByKeys()
{
$pivot = $this->getMockBuilder(Pivot::class)->setMethods(['newModelQuery'])->getMock();
$pivot = $this->getMockBuilder(Pivot::class)->setMethods(['newQueryWithoutRelationships'])->getMock();
$pivot->setPivotKeys('foreign', 'other');
$pivot->foreign = 'foreign.value';
$pivot->other = 'other.value';
$query = m::mock(stdClass::class);
$query->shouldReceive('where')->once()->with(['foreign' => 'foreign.value', 'other' => 'other.value'])->andReturn($query);
$query->shouldReceive('delete')->once()->andReturn(true);
$pivot->expects($this->once())->method('newModelQuery')->will($this->returnValue($query));
$pivot->expects($this->once())->method('newQueryWithoutRelationships')->will($this->returnValue($query));

$this->assertTrue($pivot->delete());
}
Expand Down
33 changes: 33 additions & 0 deletions tests/Integration/Database/EloquentUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ public function setUp()
$table->softDeletes();
$table->timestamps();
});

Schema::create('test_model3', function ($table) {
$table->increments('id');
$table->unsignedInteger('counter');
$table->softDeletes();
$table->timestamps();
});
}

public function testBasicUpdate()
Expand Down Expand Up @@ -107,6 +114,23 @@ public function testSoftDeleteWithJoins()

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

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

TestUpdateModel3::create([
'counter' => 0,
])->delete();

TestUpdateModel3::increment('counter');

$models = TestUpdateModel3::withoutGlobalScopes()->get();
$this->assertEquals(1, $models[0]->counter);
$this->assertEquals(0, $models[1]->counter);
}
}

class TestUpdateModel1 extends Model
Expand All @@ -124,3 +148,12 @@ class TestUpdateModel2 extends Model
protected $fillable = ['name'];
protected $dates = ['deleted_at'];
}

class TestUpdateModel3 extends Model
{
use SoftDeletes;

public $table = 'test_model3';
protected $fillable = ['counter'];
protected $dates = ['deleted_at'];
}

0 comments on commit c3f3022

Please sign in to comment.