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
While using belongsTo relations (with/load) there can be situations when there are no relations (nullable relations) to be loaded and query is "invalid". -> select `id`, `name`, `public_name` from `options` where 0 = 1 .
This similar issue can be potentially found on other relations
This bug is not noticeable if the results contains at least 1 value in the relation (if you have pagination, some pages can contain this error).
Steps To Reproduce:
Create a table with optional belongsTo relations
Build a query to get the results (all results should contain null)
Here a screenshot of a real queries
Fix
In Illuminate\Database\Eloquent\Relations\Relation add property protected bool $hasValidConstrains = true;
Change the implementation of Illuminate\Database\Eloquent\Relations\Relation::getEager
publicfunctiongetEager()
{
if ($this->hasValidConstrains) {
return$this->get();
}
returnnewCollection();
}
Change the implementation of Illuminate\Database\Eloquent\Relations\BelongsTo::addEagerConstraints
publicfunctionaddEagerConstraints(array$models)
{
// We'll grab the primary key name of the related models since it could be set to// a non-standard name and not "id". We will then construct the constraint for// our eagerly loading query so it returns the proper models from execution.$key = $this->related->getTable().'.'.$this->ownerKey;
$whereIn = $this->whereInMethod($this->related, $this->ownerKey);
$eagerModelKeys = $this->getEagerModelKeys($models);
if ($eagerModelKeys === []) {
$this->hasValidConstrains = false;
return;
}
$this->query->{$whereIn}($key, $eagerModelKeys);
}
Result
I can make PR if you consider this a bug (which I believe it is) and don't have the time to make the fix.
Thank you for your time.
Martin.
The text was updated successfully, but these errors were encountered:
Description:
with/load
) there can be situations when there are no relations (nullable relations) to be loaded and query is "invalid". ->select `id`, `name`, `public_name` from `options` where 0 = 1
.Steps To Reproduce:
Fix
Illuminate\Database\Eloquent\Relations\Relation
add propertyprotected bool $hasValidConstrains = true;
Illuminate\Database\Eloquent\Relations\Relation::getEager
Illuminate\Database\Eloquent\Relations\BelongsTo::addEagerConstraints
Result
I can make PR if you consider this a bug (which I believe it is) and don't have the time to make the fix.
Thank you for your time.
Martin.
The text was updated successfully, but these errors were encountered: