orm: return null if parent type does not exist

This commit is contained in:
Yuri Kuznetsov
2024-11-14 17:15:45 +02:00
parent 80aa052247
commit ba0784f4fd
+14 -4
View File
@@ -30,6 +30,7 @@
namespace Espo\ORM\Relation;
use Espo\ORM\BaseEntity;
use Espo\ORM\Defs\RelationDefs;
use Espo\ORM\Entity;
use Espo\ORM\EntityCollection;
use Espo\ORM\EntityManager;
@@ -225,6 +226,11 @@ class RDBRelations implements Relations
$foreignEntity = $this->getPartiallyLoadedForeignEntity($relation);
if ($foreignEntity === false) {
// Parent type does not exist. Not throwing an error deliberately.
return null;
}
if ($foreignEntity) {
return $foreignEntity;
}
@@ -293,28 +299,32 @@ class RDBRelations implements Relations
?->getType();
}
private function getPartiallyLoadedForeignEntity(string $relation): ?BaseEntity
private function getPartiallyLoadedForeignEntity(string $relation): BaseEntity|false|null
{
if (!$this->entity) {
throw new LogicException();
}
$relationDefs = $this->entityManager
$defs = $this->entityManager
->getDefs()
->getEntity($this->entity->getEntityType())
->getRelation($relation);
$relationType = $relationDefs->getType();
$relationType = $defs->getType();
$id = null;
$foreignEntityType = null;
if ($relationType === RelationType::BELONGS_TO) {
$foreignEntityType = $relationDefs->getForeignEntityType();
$foreignEntityType = $defs->getForeignEntityType();
$id = $this->entity->get($relation . 'Id');
} else if ($relationType === RelationType::BELONGS_TO_PARENT) {
$foreignEntityType = $this->entity->get($relation . 'Type');
$id = $this->entity->get($relation . 'Id');
if (!$this->entityManager->hasRepository($foreignEntityType)) {
return false;
}
}
if (!$foreignEntityType || !$id) {