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

Queried models using a hasManyThrough relationship when using chunk() on the query all have incorrect and same id #21774

Closed
xfrenette opened this issue Oct 21, 2017 · 2 comments

Comments

@xfrenette
Copy link

  • Laravel Version: 5.5.18
  • PHP Version: 7.1.0 (Windows)
  • 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.php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Country extends Model
{
    public function posts()
    {
        return $this->hasManyThrough('App\Post', 'App\User');
    }
}
<?php
// app/User.php
namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    public function country()
    {
        return $this->belongsTo('App\Country');
    }
}
<?php
// app/Post.php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

The following test would fail

<?php
namespace Tests\Unit;

use App\Country;
use App\Post;
use App\User;
use Illuminate\Support\Collection;
use Tests\TestCase;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ExampleTest extends TestCase
{
    use DatabaseTransactions;

    public function testIdsInChunk()
    {
        // Create a Country
        $country = new Country();
        $country->save();

        // Create a User
        $user = new User();
        $user->country()->associate($country);
        $user->save();

        // Create 2 Post
        for ($i = 0; $i < 2; $i++) {
            $post = new Post();
            $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());
    }
}
@sisve
Copy link
Contributor

sisve commented Oct 22, 2017

This looks related to laravel/ideas#347, and if so, is caused by multiple columns called "id" is returned in the result.

@staudenmeir
Copy link
Contributor

This has been fixed, please close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants