From 92cd8052159ff2d12f388b67f1d4e8fd9b20129d Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 28 Sep 2024 10:35:50 +0300 Subject: [PATCH] entity provider get --- .../Espo/Core/Record/EntityProvider.php | 56 +++++++++++++------ 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/application/Espo/Core/Record/EntityProvider.php b/application/Espo/Core/Record/EntityProvider.php index 7aa7ec9007..a5f77dc358 100644 --- a/application/Espo/Core/Record/EntityProvider.php +++ b/application/Espo/Core/Record/EntityProvider.php @@ -35,6 +35,8 @@ use Espo\Core\Exceptions\NotFound; use Espo\ORM\Entity; use Espo\ORM\EntityManager; +use const E_USER_DEPRECATED; + /** * Fetches entities. * @@ -64,6 +66,44 @@ class EntityProvider ->getRDBRepositoryByClass($className) ->getById($id); + return $this->processGet($entity); + } + + /** + * Fetch an entity by an entity type. + * + * @return Entity + * @throws NotFound + * @throws Forbidden + * @since 8.5.0 + */ + public function get(string $entityType, string $id): Entity + { + if (!$this->entityManager->hasRepository($entityType)) { + // @todo Remove in v9.0. + trigger_error( + 'EntityProvider::get should receive an entity type, not a class name.', + E_USER_DEPRECATED + ); + /** @phpstan-ignore-next-line */ + return $this->getByClass($entityType, $id); + } + + $entity = $this->entityManager->getEntityById($entityType, $id); + + return $this->processGet($entity); + } + + /** + * @template T of Entity + * @param ?T $entity + * @return T + * @throws Forbidden + * @throws NotFound + * @noinspection PhpDocSignatureInspection + */ + private function processGet(?Entity $entity): Entity + { if (!$entity) { throw new NotFound(); } @@ -74,20 +114,4 @@ class EntityProvider return $entity; } - - /** - * @deprecated As of v8.3. - * @todo Remove in v8.4. Change $className to $entityType. - * - * @template T of Entity - * @param class-string $className - * @return T - * @throws NotFound - * @throws Forbidden - * @noinspection PhpDocSignatureInspection - */ - public function get(string $className, string $id): Entity - { - return $this->getByClass($className, $id); - } }