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

MorphTo when getResultsByType not reset applied Global Scopes since 5.2.34 #13826

Closed
xian13 opened this issue Jun 2, 2016 · 2 comments
Closed

Comments

@xian13
Copy link
Contributor

xian13 commented Jun 2, 2016

@phroggyy your clone $this->query in the #13724 and some update in the #13737 still has some bugs related with global scopes.

In my project we create a global scopes in Model X and Model X has a MorphTo relation inside it :

class X extends Model
{

    protected static function boot()
    {
        parent::boot();
        static::addGlobalScope('x', function($builder) {
            $builder->where('x', true);
        });
    }

    public function morph()
    {
         return $this->morphTo();
    }
}

When we load the relation 'morph' what happens is that our Global Scope 'x' is included because it clone the query and still has the previous global scopes inside it.

protected function getResultsByType($type)
{
    $instance = $this->createModelByType($type);
    $key = $instance->getTable().'.'.$instance->getKeyName();
    $query = clone $this->query;
    $query->setEagerLoads($this->getEagerLoadsForInstance($instance));
    $query->setModel($instance);
    return $query->whereIn($key, $this->gatherKeysByType($type)->all())->get();
}

the $query still has the previous scopes set in model 'X'.
What is the purpose of cloning the query?
The problem isn't occurred in the 5.3.3 because it will use newQuery from the $instance.

@acasar
Copy link
Contributor

acasar commented Jun 2, 2016

@xian13 this was already reported and should be fixed. Can you try with 5.2 dev?

@xian13
Copy link
Contributor Author

xian13 commented Jun 2, 2016

@acasar I'm sorry for not doing more search, I've already tested it using 5.2 dev and it already resolve, thank you for you reply.

@xian13 xian13 closed this as completed Jun 2, 2016
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

2 participants