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

[8.x] Avoid no-op database query in Model::destroy() with empty ids #35294

Merged
merged 2 commits into from Nov 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/Illuminate/Database/Eloquent/Model.php
Expand Up @@ -1061,22 +1061,27 @@ protected function insertAndSetId(Builder $query, $attributes)
*/
public static function destroy($ids)
{
// We'll initialize a count here so we will return the total number of deletes
// for the operation. The developers can then check this number as a boolean
// type value or get this total count of records deleted for logging, etc.
$count = 0;

if ($ids instanceof BaseCollection) {
$ids = $ids->all();
}

$ids = is_array($ids) ? $ids : func_get_args();

// Avoid a no-op database query by bailing early
if (count($ids) === 0) {
return 0;
}

// We will actually pull the models from the database table and call delete on
// each of them individually so that their events get fired properly with a
// correct set of attributes in case the developers wants to check these.
$key = ($instance = new static)->getKeyName();

// We'll initialize a count here so we will return the total number of deletes
// for the operation. The developers can then check this number as a boolean
// type value or get this total count of records deleted for logging, etc.
$count = 0;

foreach ($instance->whereIn($key, $ids)->get() as $model) {
if ($model->delete()) {
$count++;
Expand Down
22 changes: 22 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Expand Up @@ -293,6 +293,17 @@ public function testDestroyMethodCallsQueryBuilderCorrectlyWithCollection()
EloquentModelDestroyStub::destroy(new Collection([1, 2, 3]));
}

public function testDestroyMethodCallsQueryBuilderCorrectlyWithMultipleArgs()
{
EloquentModelDestroyStub::destroy(1, 2, 3);
}

public function testDestroyMethodCallsQueryBuilderCorrectlyWithEmptyIds()
{
$count = EloquentModelEmptyDestroyStub::destroy([]);
$this->assertSame(0, $count);
}

public function testWithMethodCallsQueryBuilderCorrectly()
{
$result = EloquentModelWithStub::with('foo', 'bar');
Expand Down Expand Up @@ -2400,6 +2411,17 @@ public function newQuery()
}
}

class EloquentModelEmptyDestroyStub extends Model
{
public function newQuery()
{
$mock = m::mock(Builder::class);
$mock->shouldReceive('whereIn')->never();

return $mock;
}
}

class EloquentModelHydrateRawStub extends Model
{
public static function hydrate(array $items, $connection = null)
Expand Down