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

[5.4] Eloquent save-query with protected $withCount eager load on polymorphic relation fails #20640

Closed
patrickbrouwers opened this issue Aug 19, 2017 · 5 comments

Comments

@patrickbrouwers
Copy link
Contributor

patrickbrouwers commented Aug 19, 2017

  • Laravel Version: 5.4.33 (but also in 5.5-dev)
  • PHP Version: 7.1
  • Database Driver & Version: MySQL 5.7.19

Description:

Updating an Eloquent model with a default withCount eager load on a polymorphic relation fails.

Steps To Reproduce:

class Post extends Model
{
    protected $withCount = ['photos']);

    public function photos()
    {
         return $this->morphMany(Photo::class, 'imageable');
    }
}
class Photo extends Model 
{
    public function imageable()
    {
        return $this->morphTo();
    }
}
$post = Post::find(1);

// Dumps "Old name"
dump($post->name);

$post->update(['name' => 'New name']);

$post->fresh();

// expected: New name
// actual: Old name
dump($post->name);

MYSQL query:

UPDATE ... where id = App\Post

Failing Test:

(I'm happy to PR a failing test if it makes things easier)

In DatabaseEloquentIntegrationTest:

Add a withCount to the model: photos relation is polymorphic.

class EloquentTestPostWithPhotosCount extends EloquentTestPost
{
    protected $withCount = ['photos'];
}

Try to update any attribute of that model.

public function testCanUpdateWhenModelHasWithCountEagerLoad()
    {
        $post = EloquentTestPostWithPhotosCount::create(['name' => 'Some Post', 'user_id' => 1]);
        $post->photos()->create(['name' => 'Hero 1']);
        $post->photos()->create(['name' => 'Hero 2']);

        $post = EloquentTestPostWithPhotosCount::find($post->getKey());

        $post->update([
            'name' => 'New Name',
        ]);

        $databasePost = EloquentTestPostWithPhotosCount::find($post->getKey());

        $this->assertEquals($post->name, $databasePost->name);
    }

MySQL will give the following error:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 25 bind or column index out of range (SQL: update "posts" set "name" = New Name, "updated_at" = 2017-08-19 17:44:37 where "id" = Illuminate\Tests\Database\EloquentTestPostWithPhotosCount)

The query contains a strange condition:

where "id" = Illuminate\Tests\Database\EloquentTestPostWithPhotosCount

Steps To Work Around The Problem:

By not using protected $withCount = ['photos']; and only calling it via $query->withCount('photos').

However as this $withDefault property exists on the model class, I would assume it would work with updating the model as well. For my specific use-case I can work around it, but it toke me a while to figure out why my model didn't update.

Possible solution

Remove the ->with() and ->withCount() when it's a save() query: this fixes this use-case.

I tried doing this as a PR, but a lot of tests fail; I'm not sure of the impact of this.

    public function save(array $options = [])
    {
         $builder = $this->newEloquentBuilder($this->newBaseQueryBuilder());

        // Once we have the query builders, we will set the model instances so the
        // builder can easily access any information it may need from the model
        // while it is constructing and executing various queries against it.
        $query = $builder->setModel($this)

}
@patrickbrouwers patrickbrouwers changed the title [5.4] Updating with protected $withCount [5.4] Eloquent save-query with protected $withCount eager load Aug 19, 2017
@patrickbrouwers patrickbrouwers changed the title [5.4] Eloquent save-query with protected $withCount eager load [5.4] Eloquent save-query with protected $withCount eager load and polymorphic relation Aug 20, 2017
@patrickbrouwers patrickbrouwers changed the title [5.4] Eloquent save-query with protected $withCount eager load and polymorphic relation [5.4] Eloquent save-query with protected $withCount eager load on polymorphic relation Aug 20, 2017
@patrickbrouwers patrickbrouwers changed the title [5.4] Eloquent save-query with protected $withCount eager load on polymorphic relation [5.4] Eloquent save-query with protected $withCount eager load on polymorphic relation fails Aug 21, 2017
@brunogaspar
Copy link
Contributor

Having the exact same issue.

@ghost
Copy link

ghost commented Aug 30, 2017

Me too. (Laravel 5.5)

@brunogaspar
Copy link
Contributor

@themsaid Any chance to look into this issue please?

@themsaid
Copy link
Member

This is fixed in a recent PR

@brunogaspar
Copy link
Contributor

Awesome, thanks @themsaid !

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

3 participants