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

[9.x] Optimize findMany of BelongsToMany πŸƒπŸ»β€β™‚οΈ #45745

Merged
merged 1 commit into from
Jan 22, 2023
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
8 changes: 3 additions & 5 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -688,8 +688,8 @@ public function findMany($ids, $columns = ['*'])
return $this->getRelated()->newCollection();
}

return $this->whereIn(
$this->getRelated()->getQualifiedKeyName(), $this->parseIds($ids)
return $this->whereKey(
$this->parseIds($ids)
)->get($columns);
}

Expand Down Expand Up @@ -1153,8 +1153,6 @@ protected function guessInverseRelation()
*/
public function touch()
{
$key = $this->getRelated()->getKeyName();

$columns = [
$this->related->getUpdatedAtColumn() => $this->related->freshTimestampString(),
];
Expand All @@ -1163,7 +1161,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()->newQueryWithoutRelationships()->whereIn($key, $ids)->update($columns);
$this->getRelated()->newQueryWithoutRelationships()->whereKey($ids)->update($columns);
}
}

Expand Down
78 changes: 78 additions & 0 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,48 @@ public function testFindMethod()
$this->assertCount(2, $post->tags()->findMany(new Collection([$tag->id, $tag2->id])));
}

public function testFindMethodStringyKey()
{
Schema::create('post_string_key', function (Blueprint $table) {
$table->string('id', 1)->primary();
$table->string('title', 10);
});

Schema::create('tag_string_key', function (Blueprint $table) {
$table->string('id', 1)->primary();
$table->string('title', 10);
});

Schema::create('post_tag_string_key', function (Blueprint $table) {
$table->id();
$table->string('post_id', 1);
$table->string('tag_id', 1);
});

$post = PostStringPrimaryKey::query()->create([
'id' => 'a',
'title' => Str::random(10),
]);

$tag = TagStringPrimaryKey::query()->create([
'id' => 'b',
'title' => Str::random(10),
]);

$tag2 = TagStringPrimaryKey::query()->create([
'id' => 'c',
'title' => Str::random(10),
]);

$post->tags()->attach(TagStringPrimaryKey::all());

$this->assertEquals($tag2->name, $post->tags()->find($tag2->id)->name);
$this->assertCount(0, $post->tags()->findMany([]));
$this->assertCount(2, $post->tags()->findMany([$tag->id, $tag2->id]));
$this->assertCount(0, $post->tags()->findMany(new Collection));
$this->assertCount(2, $post->tags()->findMany(new Collection([$tag->id, $tag2->id])));
}

public function testFindOrFailMethod()
{
$this->expectException(ModelNotFoundException::class);
Expand Down Expand Up @@ -1221,6 +1263,42 @@ public function postsWithCustomPivot()
}
}

class PostStringPrimaryKey extends Model
{
public $incrementing = false;

public $timestamps = false;

protected $table = 'post_string_key';

protected $keyType = 'string';

protected $fillable = ['title', 'id'];

public function tags()
{
return $this->belongsToMany(TagStringPrimaryKey::class, 'post_tag_string_key', 'post_id', 'tag_id');
}
}

class TagStringPrimaryKey extends Model
{
public $incrementing = false;

public $timestamps = false;

protected $table = 'tag_string_key';

protected $keyType = 'string';

protected $fillable = ['title', 'id'];

public function posts()
{
return $this->belongsToMany(PostStringPrimaryKey::class, 'post_tag_string_key', 'tag_id', 'post_id');
}
}

class Post extends Model
{
public $table = 'posts';
Expand Down