diff --git a/application/Espo/Core/Record/Service.php b/application/Espo/Core/Record/Service.php index 77ff655fd1..1ee81c2e92 100644 --- a/application/Espo/Core/Record/Service.php +++ b/application/Espo/Core/Record/Service.php @@ -41,7 +41,7 @@ use Espo\Core\Exceptions\{ use Espo\ORM\{ Entity, - Repository\Repository, + Repository\RDBRepository, Collection, EntityManager, }; @@ -211,9 +211,9 @@ class Service implements Crud, $this->entityType = $entityType; } - protected function getRepository(): Repository + protected function getRepository(): RDBRepository { - return $this->entityManager->getRepository($this->entityType); + return $this->entityManager->getRDBRepository($this->entityType); } public function processActionHistoryRecord(string $action, Entity $entity): void @@ -273,16 +273,11 @@ class Service implements Crud, /** * Get an entity by ID. Access control check is performed. - * If ID is not specified then it will return an empty entity. * * @throws ForbiddenSilent If no read access. */ - public function getEntity(?string $id = null): ?Entity + public function getEntity(string $id): ?Entity { - if ($id === null) { - return $this->getRepository()->getNew(); - } - $entity = $this->getRepository()->getById($id); if (!$entity && $this->user->isAdmin()) { @@ -566,7 +561,7 @@ class Service implements Crud, throw new ForbiddenSilent(); } - $entity = $this->getRepository()->get(); + $entity = $this->getRepository()->getNew(); $this->filterCreateInput($data); @@ -607,17 +602,16 @@ class Service implements Crud, throw new ForbiddenSilent(); } - if (empty($id)) { + if (!$id) { throw new BadRequest("ID is empty."); } $this->filterUpdateInput($data); - if ($this->getEntityBeforeUpdate) { - $entity = $this->getEntity($id); - } else { - $entity = $this->getRepository()->get($id); - } + $entity = + $this->getEntityBeforeUpdate ? + $this->getEntity($id) : + $this->getRepository()->getById($id); if (!$entity) { throw new NotFound("Record {$id} not found."); @@ -663,11 +657,11 @@ class Service implements Crud, throw new ForbiddenSilent(); } - if (empty($id)) { + if (!$id) { throw new BadRequest("ID is empty."); } - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound("Record {$id} not found."); @@ -810,7 +804,7 @@ class Service implements Crud, throw new ForbiddenSilent("No access."); } - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound(); @@ -934,7 +928,7 @@ class Service implements Crud, $this->processForbiddenLinkEditCheck($link); - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound(); @@ -1002,7 +996,7 @@ class Service implements Crud, $this->processForbiddenLinkEditCheck($link); - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound(); @@ -1060,7 +1054,7 @@ class Service implements Crud, throw new NotFound(); } - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound(); @@ -1113,7 +1107,7 @@ class Service implements Crud, throw new NotFound(); } - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound(); @@ -1164,7 +1158,7 @@ class Service implements Crud, $this->processForbiddenLinkEditCheck($link); - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound(); @@ -1299,7 +1293,7 @@ class Service implements Crud, throw new Forbidden(); } - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFoundSilent(); @@ -1326,7 +1320,7 @@ class Service implements Crud, */ public function unfollow(string $id, ?string $userId = null): void { - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFoundSilent(); @@ -1439,7 +1433,7 @@ class Service implements Crud, protected function findLinkedFollowers(string $id, SearchParams $params): RecordCollection { - $entity = $this->getRepository()->get($id); + $entity = $this->getRepository()->getById($id); if (!$entity) { throw new NotFound(); diff --git a/application/Espo/ORM/EntityManager.php b/application/Espo/ORM/EntityManager.php index bd8349731f..c4d8295536 100644 --- a/application/Espo/ORM/EntityManager.php +++ b/application/Espo/ORM/EntityManager.php @@ -331,7 +331,7 @@ class EntityManager /** * Get an entity. If $id is null, a new entity instance is created. - * If an entity with a specified $id does not exist, then NULL is returned. + * If an entity with a specified ID does not exist, then NULL is returned. */ public function getEntity(string $entityType, ?string $id = null): ?Entity { @@ -339,11 +339,31 @@ class EntityManager throw new RuntimeException("ORM: Repository '{$entityType}' does not exist."); } - return $this->getRepository($entityType)->get($id); + if ($id === null) { + return $this->getRepository($entityType)->getNew(); + } + + return $this->getRepository($entityType)->getById($id); } /** - * Store an entity (in database). + * Create a new entity instance (w/o storing to DB). + */ + public function getNewEntity(string $entityType): Entity + { + return $this->getEntity($entityType); + } + + /** + * Get an entity by ID. If an entity does not exist, NULL is returned. + */ + public function getEntityById(string $entityType, string $id): ?Entity + { + return $this->getEntity($entityType, $id); + } + + /** + * Store an entity. * * @return Deprecated. @todo To be changed to void in 6.3. */ @@ -367,7 +387,7 @@ class EntityManager } /** - * Create entity (store it in a database). + * Create entity (and store to database). * * @param StdClass|array $data Entity attributes. */ @@ -381,18 +401,6 @@ class EntityManager return $entity; } - /** - * Fetch an entity (from a database). - */ - public function fetchEntity(string $entityType, string $id): ?Entity - { - if (empty($id)) { - return null; - } - - return $this->getEntity($entityType, $id); - } - /** * Check whether a repository for a specific entity type exist. */ diff --git a/application/Espo/ORM/Repository/RDBRepository.php b/application/Espo/ORM/Repository/RDBRepository.php index 11e33143a0..9372616a82 100644 --- a/application/Espo/ORM/Repository/RDBRepository.php +++ b/application/Espo/ORM/Repository/RDBRepository.php @@ -72,17 +72,13 @@ class RDBRepository extends Repository /** * Get a new entity. */ - public function getNew(): ?Entity + public function getNew(): Entity { $entity = $this->entityFactory->create($this->entityType); - if ($entity) { - $entity->populateDefaults(); + $entity->populateDefaults(); - return $entity; - } - - return null; + return $entity; } /** @@ -90,7 +86,8 @@ class RDBRepository extends Repository */ public function getById(string $id): ?Entity { - $select = $this->entityManager->getQueryBuilder() + $selectQuery = $this->entityManager + ->getQueryBuilder() ->select() ->from($this->entityType) ->where([ @@ -98,9 +95,12 @@ class RDBRepository extends Repository ]) ->build(); - return $this->getMapper()->selectOne($select); + return $this->getMapper()->selectOne($selectQuery); } + /** + * Get an entity. If ID is NULL, a new entity is returned. + */ public function get(?string $id = null): ?Entity { if (is_null($id)) { diff --git a/application/Espo/ORM/Repository/Repository.php b/application/Espo/ORM/Repository/Repository.php index e8016832c1..73a4c5e7fd 100644 --- a/application/Espo/ORM/Repository/Repository.php +++ b/application/Espo/ORM/Repository/Repository.php @@ -40,10 +40,19 @@ use Espo\ORM\{ */ abstract class Repository { + /** + * @var EntityFactory + */ protected $entityFactory; + /** + * @var EntityManager + */ protected $entityManager; + /** + * @var string + */ protected $entityType; public function __construct(string $entityType, EntityManager $entityManager, EntityFactory $entityFactory) @@ -69,9 +78,14 @@ abstract class Repository } /** - * Get an entity. If $id is NULL, a new entity is returned. + * Get a new entity. */ - abstract public function get(?string $id = null): ?Entity; + abstract public function getNew(): Entity; + + /** + * Fetch an entity by ID. + */ + abstract public function getById(string $id): ?Entity; /** * Store an entity. diff --git a/application/Espo/Repositories/Preferences.php b/application/Espo/Repositories/Preferences.php index d9d76317c9..568156efc0 100644 --- a/application/Espo/Repositories/Preferences.php +++ b/application/Espo/Repositories/Preferences.php @@ -58,13 +58,15 @@ class Preferences extends Repository implements protected $entityType = 'Preferences'; - public function get(?string $id = null): ?Entity + public function getNew(): Entity { - if (!$id) { - return $this->entityFactory->create('Preferences'); - } + return $this->entityFactory->create('Preferences'); + } + public function getById(string $id): ?Entity + { $entity = $this->entityFactory->create('Preferences'); + $entity->id = $id; if (!isset($this->data[$id])) { @@ -80,6 +82,15 @@ class Preferences extends Repository implements return $entity; } + public function get(?string $id = null): ?Entity + { + if ($id === null) { + return $this->getNew(); + } + + return $this->getById($id); + } + protected function loadData(string $id) { $data = null; diff --git a/application/Espo/Repositories/UniqueId.php b/application/Espo/Repositories/UniqueId.php index cbf97e5d0e..db19362b1c 100644 --- a/application/Espo/Repositories/UniqueId.php +++ b/application/Espo/Repositories/UniqueId.php @@ -37,7 +37,7 @@ class UniqueId extends \Espo\Core\Repositories\Database { protected $hooksDisabled = true; - public function getNew(): ?Entity + public function getNew(): Entity { $entity = parent::getNew(); diff --git a/application/Espo/Services/DashboardTemplate.php b/application/Espo/Services/DashboardTemplate.php index 87cd19e22f..f79a46b114 100644 --- a/application/Espo/Services/DashboardTemplate.php +++ b/application/Espo/Services/DashboardTemplate.php @@ -87,14 +87,14 @@ class DashboardTemplate extends Record public function deployToUsers(string $id, array $userIdList, bool $append = false): void { - $template = $this->getEntityManager()->fetchEntity('DashboardTemplate', $id); + $template = $this->getEntityManager()->getEntity('DashboardTemplate', $id); if (!$template) { throw new NotFound(); } foreach ($userIdList as $userId) { - $user = $this->getEntityManager()->fetchEntity('User', $userId); + $user = $this->getEntityManager()->getEntity('User', $userId); if ($user) { if ($user->isPortal() || $user->isApi()) { @@ -104,7 +104,7 @@ class DashboardTemplate extends Record } foreach ($userIdList as $userId) { - $preferences = $this->getEntityManager()->fetchEntity('Preferences', $userId); + $preferences = $this->getEntityManager()->getEntity('Preferences', $userId); if (!$preferences) { continue; @@ -118,13 +118,13 @@ class DashboardTemplate extends Record public function deployToTeam(string $id, string $teamId, bool $append = false): void { - $template = $this->getEntityManager()->fetchEntity('DashboardTemplate', $id); + $template = $this->getEntityManager()->getEntity('DashboardTemplate', $id); if (!$template) { throw new NotFound(); } - $team = $this->getEntityManager()->fetchEntity('Team', $teamId); + $team = $this->getEntityManager()->getEntity('Team', $teamId); if (!$team) { throw new NotFound(); @@ -140,7 +140,7 @@ class DashboardTemplate extends Record ->find(); foreach ($userList as $user) { - $preferences = $this->getEntityManager()->fetchEntity('Preferences', $user->id); + $preferences = $this->getEntityManager()->getEntity('Preferences', $user->id); if (!$preferences) { continue;