public function __construct(EntityManager $manager) { $this->manager = $manager; $this->uow = $manager->getUnitOfWork(); $proxyGenerator = new ProxyGenerator($this->manager->getConfiguration()->getProxyDir(), $this->manager->getConfiguration()->getProxyNamespace()); parent::__construct($proxyGenerator, $this->manager->getMetadataFactory(), $this->manager->getConfiguration()->isAutogenerateProxies()); }
/** * @param string $field * @param \StdClass $source * @param object $entity * * @return array|Proxy|object * @throws HydrationException * @throws MappingException */ private function hydrateAssociation($field, $entity, $source) { $accessor = new PropertyAccessor(); $targetClassName = $this->metadata->getAssociationTargetClass($field); $mapping = $this->metadata->getAssociationMapping($field); $targetPersister = $this->manager->getUnitOfWork()->getEntityPersister($targetClassName); $targetMetadata = $this->manager->getClassMetadata($mapping['target']); $apiField = $mapping['api_field']; $field = $mapping['field']; if ($this->metadata->isSingleValuedAssociation($field)) { $identifiers = $this->metadata->getIdentifierValues($entity); if ($mapping['isOwningSide']) { try { $value = $accessor->getValue($source, $apiField); } catch (NoSuchPropertyException $exception) { if ($mapping['nullable']) { return null; } throw new HydrationException(sprintf('Api field %s for property %s does not present in response', $apiField, $field)); } if ($targetMetadata->isIdentifierComposite()) { throw new HydrationException('Composite references not supported'); } $targetIdsNames = $targetMetadata->getIdentifierFieldNames(); $targetIdName = array_shift($targetIdsNames); $type = $this->manager->getConfiguration()->getTypeRegistry()->get($targetMetadata->getTypeOfField($targetIdName)); $identifiers = [$targetIdName => $type->fromApiValue($value)]; } return $targetPersister->getToOneEntity($mapping, $entity, $identifiers); } if ($this->metadata->isCollectionValuedAssociation($field)) { return $targetPersister->getOneToManyCollection($mapping, $entity); } throw new MappingException('Invalid metadata association type'); }
public function getToOneEntity(array $mapping, $sourceEntity, array $identifiers) { $metadata = $this->manager->getClassMetadata(get_class($sourceEntity)); if (!$mapping['isOwningSide']) { $identifiers = $metadata->getIdentifierValues($sourceEntity); } return $this->manager->getReference($mapping['target'], $identifiers); }
/** * Tries to find an entity with the given identifier in the identity map of * this UnitOfWork. * * @param mixed $id The entity identifier to look for. * @param string $rootClassName The name of the root class of the mapped entity hierarchy. * * @return object|bool Returns the entity with the specified identifier if it exists in * this UnitOfWork, FALSE otherwise. */ public function tryGetById($id, $rootClassName) { /** @var EntityMetadata $metadata */ $metadata = $this->manager->getClassMetadata($rootClassName); $idHash = implode(' ', (array) $this->identifierFlattener->flattenIdentifier($metadata, $id)); if (isset($this->identityMap[$rootClassName][$idHash])) { return $this->identityMap[$rootClassName][$idHash]; } return false; }
/** * @return RpcClientInterface */ protected function getClient() { return $this->manager->getConfiguration()->getRegistry()->get($this->metadata->getClientName()); }
/** {@inheritdoc} */ protected function initialize() { $this->driver = $this->manager->getConfiguration()->getDriver(); $this->initialized = true; }