-
Notifications
You must be signed in to change notification settings - Fork 11k
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 fails on eager loading relation with numeric string key #34331
Comments
Will need some help here from people using SqlServer. |
My quick look trough query grammar code suggests PHP auto-casting to integer, which SqlServer doesn't like. Simplest solution would be to wrap all query parameters in quotes, but I'm not sure it's fail-proof. |
I think this is possibly fixable. The
public function where($column, $operator = null, $value = null, $boolean = 'and')
{
if ($column instanceof Closure && is_null($operator)) {
$column($query = $this->model->newQueryWithoutRelationships());
$this->query->addNestedWhereQuery($query->getQuery(), $boolean);
} else {
$this->query->where(...func_get_args());
}
return $this;
} Since a nested query will almost always wrap back around to the final if.. $this->query->where(...func_get_args()); You could probably get away with something like.... $arguments = func_get_args();
if (count($arguments) === 1 && is_array($arguments)) {
$key = null;
if (array_key_exists($this->getModel()->getKeyName(), $arguments[0])) {
$key = $arguments[0][$this->getModel()->getKeyName()];
unset($arguments[0][$this->getModel()->getKeyName()]);
} else if (array_key_exists($this->getModel()->getQualifiedKeyName(), $arguments[0])) {
$key = $arguments[0][$this->getModel()->getQualifiedKeyName()];
unset($arguments[0][$this->getModel()->getQualifiedKeyName()]);
}
if ($key !== null) {
$this->whereKey($key);
if (empty($arguments[0])) {
return $this;
}
}
} else {
if ($arguments[0] === $this->getModel()->getKeyName()) {
$this->whereKey(array_pop($arguments));
return $this;
}
}
$this->query->where(...$arguments);
return $this; This is just a pure example that I threw together now, it maybe needs tidying up. This would make sure that anywhere clause that included the key is cast correctly. Line 196 of the if ($id !== null && $this->model->getKeyType() === 'string') {
$id = (string) $id;
} This also supports the fully qualified name when passing an array, such as from the |
I think this may be fixed by #34531 |
Can confirm fix working on SQL server. |
Description:
Laravel fails to eager load
morphTo()
relationship when morphed model's$keyType = 'string'
, but key itself is numeric.Steps To Reproduce:
From example project https://github.com/saulens22/laravel8mssql/blob/master/tests/Feature/LaravelMssqlTest.php
Error:
The text was updated successfully, but these errors were encountered: