[v3] legacy_model_binding with some model create by withDefault has error #5979
-
|
I trying upgrade to beta.4 now, but have some question. public function newsData()
{
return $this->hasOne(NewsMainData::class, 'news_id', 'news_id')->withDefault([
'cover_description' => '',
'headline' => '',
'show_menu' => false,
]);
}The model still not in db , EloquentModelSynth@loadModel will return null; then get error like this
So I add this in EloquentModelSynth@loadModel , this error is gone if(empty($model)){
$model = new $class();
}I still working with other test so can't sure it is ok. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 3 replies
-
|
@fripig thanks for reporting! Any chance you could submit a failing test PR so we can investigate? Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
Ok, I discovered that by changing the way the model requests the query, the error of an empty or missing model in the database disappears. Like this: protected function loadModel($meta): ?Model
{
$class = $meta['class'];
// If no alias found, this returns `null`
$aliasClass = Relation::getMorphedModel($class);
if (!is_null($aliasClass)) {
$class = $aliasClass;
}
if (isset($meta['key'])) {
$model = new $class;
if (isset($meta['connection'])) {
$model->setConnection($meta['connection']);
}
$query = $model->newQueryForRestoration($meta['key']);
if (isset($meta['relations'])) {
$query->with($meta['relations']);
}
if ($query->first() != null)
$model = $query->first();
} else {
$model = new $class();
}
return $model;
}I was trying to resolve the error:
|
Beta Was this translation helpful? Give feedback.
-
|
Hi guys, now we are migrating our project from V2 -> V3 and we are encountering this issue, We are using V3.5. Do you know if was fixed or if there is a way to resolve it so we don't get this nasty error? I saw that was a PR #8047 that was closed. The solution is fixing the issue but from what I read is not the best way to handle that. Best regards |
Beta Was this translation helpful? Give feedback.
@fripig thanks for reporting! Any chance you could submit a failing test PR so we can investigate? Thanks!