Make hasOne / hasOneThrough and morphOne nullable. (#864)

This commit is contained in:
leo108
2020-03-31 14:40:50 +02:00
committed by GitHub
parent 1d1484025c
commit 18588eacf3
3 changed files with 29 additions and 8 deletions
+16 -6
View File
@@ -563,7 +563,7 @@ class ModelsCommand extends Command
true,
null,
'',
$this->isRelationForeignKeyNullable($relationObj)
$this->isRelationNullable($relation, $relationObj)
);
}
}
@@ -575,22 +575,32 @@ class ModelsCommand extends Command
}
/**
* Check if the foreign key of the relation is nullable
* Check if the relation is nullable
*
* @param Relation $relation
* @param string $relation
* @param Relation $relationObj
*
* @return bool
*/
private function isRelationForeignKeyNullable(Relation $relation)
private function isRelationNullable(string $relation, Relation $relationObj): bool
{
$reflectionObj = new \ReflectionObject($relation);
$reflectionObj = new \ReflectionObject($relationObj);
if (in_array($relation, ['hasOne', 'hasOneThrough', 'morphOne'], true)) {
$defaultProp = $reflectionObj->getProperty('withDefault');
$defaultProp->setAccessible(true);
return !$defaultProp->getValue($relationObj);
}
if (!$reflectionObj->hasProperty('foreignKey')) {
return false;
}
$fkProp = $reflectionObj->getProperty('foreignKey');
$fkProp->setAccessible(true);
return isset($this->nullableColumns[$fkProp->getValue($relation)]);
return isset($this->nullableColumns[$fkProp->getValue($relationObj)]);
}
/**