*/ private $relatedEntitiesCacheMap = []; /** * @return mixed */ public function fetch(Entity $entity, string $attribute, bool $getFetchedAttribute = false) { if (strpos($attribute, '.') !== false) { $arr = explode('.', $attribute); $key = $this->buildKey($entity, $arr[0]); if (!array_key_exists($key, $this->relatedEntitiesCacheMap)) { // @todo Use getRelation. $this->relatedEntitiesCacheMap[$key] = $entity->get($arr[0]); } $relatedEntity = $this->relatedEntitiesCacheMap[$key]; if ( $relatedEntity && ($relatedEntity instanceof Entity) && count($arr) > 1 ) { return $this->fetch($relatedEntity, $arr[1]); } return null; } $methodName = 'get'; if ($getFetchedAttribute) { $methodName = 'getFetched'; } if ( $entity instanceof CoreEntity && $entity->getAttributeParam($attribute, 'isParentName') && $methodName == 'get' ) { $relationName = $entity->getAttributeParam($attribute, 'relation'); $parent = $parent = $entity->get($relationName); if ($parent) { return $parent->get('name'); } } else if ( $entity instanceof CoreEntity && $entity->getAttributeParam($attribute, 'isLinkMultipleIdList') && $methodName == 'get' ) { $relationName = $entity->getAttributeParam($attribute, 'relation'); if (!$entity->has($attribute)) { $entity->loadLinkMultipleField($relationName); } } if ($methodName === 'getFetched') { return $entity->getFetched($attribute); } return $entity->get($attribute); } public function resetRuntimeCache(): void { $this->relatedEntitiesCacheMap = []; } private function buildKey(Entity $entity, string $link): string { return spl_object_hash($entity) . '-' . $link; } }