You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Database Driver & Version: MySQL 5.7.18, for Linux (x86_64) (through Vagrant)
Description:
Summary: When querying models through a hasManyThrough relationship and using chunk() on the query, all the retrieved models have the same id (and probably a non-existent id for the model), even though their other attributes have the correct values. It creates a problem when eager loading other relations, since they all have the same id.
Maybe the wrong column is used to fill the id attribute.
No problem when retrieving the same set without using chunk().
Example: (see below for details) If I use the hasManyThrough example from the doc, then the following 2 codes would echo different values (suppose we have less than 100 posts):
// Without `chunk()`var_dump($country->posts()->get()->pluck('id')->toArray());
// Example output: [1, 2, 3]
// With `chunk()` (suppose we have less than 100 posts)$country->posts()->chunk(100, function ($posts) {
var_dump($posts->pluck('id')->toArray());
// Example output: [1, 1, 1]
});
Note that the other attributes are correctly set in both cases (ex: $post->title).
Steps To Reproduce:
With the following tables
posts
id
user_id
users
id
country_id
countries
id
and the following models
<?php// app/Country.phpnamespaceApp;
useIlluminate\Database\Eloquent\Model;
class Country extends Model
{
publicfunctionposts()
{
return$this->hasManyThrough('App\Post', 'App\User');
}
}
<?php// app/User.phpnamespaceApp;
useIlluminate\Database\Eloquent\Model;
class User extends Model
{
publicfunctioncountry()
{
return$this->belongsTo('App\Country');
}
}
<?php// app/Post.phpnamespaceApp;
useIlluminate\Database\Eloquent\Model;
class Post extends Model
{
publicfunctionuser()
{
return$this->belongsTo('App\User');
}
}
The following test would fail
<?phpnamespaceTests\Unit;
useApp\Country;
useApp\Post;
useApp\User;
useIlluminate\Support\Collection;
useTests\TestCase;
useIlluminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
use DatabaseTransactions;
publicfunctiontestIdsInChunk()
{
// Create a Country$country = newCountry();
$country->save();
// Create a User$user = newUser();
$user->country()->associate($country);
$user->save();
// Create 2 Postfor ($i = 0; $i < 2; $i++) {
$post = newPost();
$post->user()->associate($user);
$post->save();
}
// Get Post ids with a `get()`$ids_without_chunk = $country->posts()->get()->pluck('id');
// Get Post ids with a `chunk()`$ids_with_chunk = collect();
$country->posts()->chunk(100, function ($posts) use (&$ids_with_chunk) {
$ids_with_chunk = $ids_with_chunk->merge($posts->pluck('id'));
});
// Compare$this->assertEquals($ids_with_chunk->toArray(), $ids_without_chunk->toArray());
}
}
The text was updated successfully, but these errors were encountered:
Description:
Summary: When querying models through a
hasManyThrough
relationship and usingchunk()
on the query, all the retrieved models have the same id (and probably a non-existent id for the model), even though their other attributes have the correct values. It creates a problem when eager loading other relations, since they all have the same id.Maybe the wrong column is used to fill the
id
attribute.No problem when retrieving the same set without using
chunk()
.Example: (see below for details) If I use the
hasManyThrough
example from the doc, then the following 2 codes would echo different values (suppose we have less than 100 posts):Note that the other attributes are correctly set in both cases (ex: $post->title).
Steps To Reproduce:
With the following tables
and the following models
The following test would fail
The text was updated successfully, but these errors were encountered: