Closed SlyDave closed 4 years ago
Code in our project that it's breaking on is a fairly complex chain of filtered with
statements and some scopes, but nothing out of the ordinary.
All of the child models have the Compoships trait too.
$assignments = UserVesselAssignment::with(
[
'user.latestHistory' => function (HasOne $history) use ($date) {
return $history->forMonth($date);
},
'latestUserVesselHistory' => function (HasOne $history) use ($date) {
return $history->forMonth($date)->with('department', 'departmentPosition');
},
]
)->forVessel($vessel)->forMonth($date)->get();
It's the Laravel key optimisation not working with compound keys, it appears the changes in 1.1.17 are not working correctly? (at least for relationship conditions) from what I can tell...
The Laravel 7 key optimisation:
protected function whereInMethod(Model $model, $key)
{
return $model->getKeyName() === last(explode('.', $key))
&& in_array($model->getKeyType(), ['int', 'integer'])
? 'whereIntegerInRaw'
: 'whereIn';
}
The removal of addEagerConstraints()
from src/Database/Eloquent/Relations/HasOneOrMany.php is how the breaking is occurring.
As without that method is calls the parent version, which in turn calls whereInMethod
which has the last(explode('/', $key))
as noted above.
Unlike the BelongsTo, which is working because it has a addEagerConstraints()
override
Hi @SlyDave Can you PR the fix?
I've added a version of whereInMethod()
to HasOneOrMany
that works with compound keys, delegating to the parent if key isn't an array. See PR for details.
Please check 1.1.18. Thanks for the contribution.
Running 1.1.16 everything works fine.
Updating to 1.1.17 results in:
ErrorException explode() expects parameter 2 to be string, array given
Which appears to be occurring @
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Relations/Relation.php:319
It's as if the custom logic provided by compoships stops working and it reverts to using eloquent's implementation...