Checks whether an entity is registered in the identity map of this UnitOfWork.
public isInIdentityMap ( object $entity ) : boolean | ||
$entity | object | |
리턴 | boolean |
/** * {@inheritdoc} */ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, $entity) { $data = $this->uow->getOriginalEntityData($entity); $data = array_merge($data, $key->identifier); // why update has no identifier values ? foreach ($metadata->associationMappings as $name => $assoc) { if (!isset($data[$name])) { continue; } if (!isset($assoc['cache']) || !($assoc['type'] & ClassMetadata::TO_ONE)) { unset($data[$name]); continue; } if (!isset($assoc['id'])) { $targetClass = ClassUtils::getClass($data[$name]); $targetId = $this->uow->getEntityIdentifier($data[$name]); $data[$name] = new AssociationCacheEntry($targetClass, $targetId); continue; } // handle association identifier $targetId = is_object($data[$name]) && $this->uow->isInIdentityMap($data[$name]) ? $this->uow->getEntityIdentifier($data[$name]) : $data[$name]; // @TODO - fix it ! // handle UnitOfWork#createEntity hash generation if (!is_array($targetId)) { $data[reset($assoc['joinColumnFieldNames'])] = $targetId; $targetEntity = $this->em->getClassMetadata($assoc['targetEntity']); $targetId = array($targetEntity->identifier[0] => $targetId); } $data[$name] = new AssociationCacheEntry($assoc['targetEntity'], $targetId); } return new EntityCacheEntry($metadata->name, $data); }
/** * convert foreign identifiers into scalar foreign key values to avoid object to string conversion failures. * * @param ClassMetadata $class * @param array $id * * @return array */ public function flattenIdentifier(ClassMetadata $class, array $id) { $flatId = array(); foreach ($class->identifier as $field) { if (isset($class->associationMappings[$field]) && isset($id[$field]) && is_object($id[$field])) { /* @var $targetClassMetadata ClassMetadata */ $targetClassMetadata = $this->metadataFactory->getMetadataFor($class->associationMappings[$field]['targetEntity']); if ($this->unitOfWork->isInIdentityMap($id[$field])) { $associatedId = $this->flattenIdentifier($targetClassMetadata, $this->unitOfWork->getEntityIdentifier($id[$field])); } else { $associatedId = $this->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($id[$field])); } $flatId[$field] = implode(' ', $associatedId); } elseif (isset($class->associationMappings[$field])) { $associatedId = array(); foreach ($class->associationMappings[$field]['joinColumns'] as $joinColumn) { $associatedId[] = $id[$joinColumn['name']]; } $flatId[$field] = implode(' ', $associatedId); } else { $flatId[$field] = $id[$field]; } } return $flatId; }
/** * @param Object $entity * @return string */ public function transform($entity) { if (null === $entity || '' === $entity) { return 'null'; } if (!is_object($entity)) { throw new UnexpectedTypeException($entity, 'object'); } if (!$this->unitOfWork->isInIdentityMap($entity)) { throw new InvalidArgumentException('Entities passed to the choice field must be managed'); } return $entity->getId(); }
/** * @param ClassMetadata $class * @param array $entityData * @param string $revType * @param boolean $draft */ private function saveRevisionEntityData($class, $entityData, $revType) { $version = $this->getRevisionId(); $params = array($version ? $version : 1, $revType); $types = array(Type::INTEGER, Type::STRING); $fields = array(); foreach ($class->associationMappings as $field => $assoc) { if ($class->isInheritanceTypeJoined() && $class->isInheritedAssociation($field)) { continue; } if (!(($assoc['type'] & ClassMetadata::TO_ONE) > 0 && $assoc['isOwningSide'])) { continue; } if (!$this->annotationReader->isPropertyRevised($class->name, $field)) { continue; } $data = isset($entityData[$field]) ? $entityData[$field] : null; $relatedId = false; if ($data !== null && $this->uow->isInIdentityMap($data)) { $relatedId = $this->uow->getEntityIdentifier($data); } $targetClass = $this->em->getClassMetadata($assoc['targetEntity']); foreach ($assoc['sourceToTargetKeyColumns'] as $sourceColumn => $targetColumn) { $fields[$sourceColumn] = true; if ($data === null) { $params[] = null; $types[] = \PDO::PARAM_STR; } else { $params[] = $relatedId ? $relatedId[$targetClass->fieldNames[$targetColumn]] : null; $types[] = $targetClass->getTypeOfColumn($targetColumn); } } } foreach ($class->fieldNames as $field) { if (array_key_exists($field, $fields)) { continue; } if ($class->isInheritanceTypeJoined() && $class->isInheritedField($field) && !$class->isIdentifier($field)) { continue; } if (!$this->annotationReader->isPropertyRevised($class->name, $field) && !$class->isIdentifier($field)) { continue; } $params[] = isset($entityData[$field]) ? $entityData[$field] : null; $types[] = $class->fieldMappings[$field]['type']; } if ($class->isInheritanceTypeJoined() && $class->name == $class->rootEntityName) { $params[] = $entityData[$class->discriminatorColumn['name']]; $types[] = $class->discriminatorColumn['type']; } if ($class->isInheritanceTypeJoined() && $class->name != $class->rootEntityName) { $entityData[$class->discriminatorColumn['name']] = $class->discriminatorValue; $this->saveRevisionEntityData($this->em->getClassMetadata($class->rootEntityName), $entityData, $revType); } $this->conn->executeUpdate($this->getRevisionSQL($class), $params, $types); }
/** * {@inheritdoc} */ public function buildCacheEntry(ClassMetadata $metadata, EntityCacheKey $key, $entity) { $data = $this->uow->getOriginalEntityData($entity); $data = array_merge($data, $metadata->getIdentifierValues($entity)); // why update has no identifier values ? foreach ($metadata->associationMappings as $name => $assoc) { if (!isset($data[$name])) { continue; } if (!($assoc['type'] & ClassMetadata::TO_ONE)) { unset($data[$name]); continue; } if (!isset($assoc['cache'])) { $targetClassMetadata = $this->em->getClassMetadata($assoc['targetEntity']); $owningAssociation = !$assoc['isOwningSide'] ? $targetClassMetadata->associationMappings[$assoc['mappedBy']] : $assoc; $associationIds = $this->identifierFlattener->flattenIdentifier($targetClassMetadata, $targetClassMetadata->getIdentifierValues($data[$name])); unset($data[$name]); foreach ($associationIds as $fieldName => $fieldValue) { if (isset($targetClassMetadata->fieldMappings[$fieldName])) { $fieldMapping = $targetClassMetadata->fieldMappings[$fieldName]; $data[$owningAssociation['targetToSourceKeyColumns'][$fieldMapping['columnName']]] = $fieldValue; continue; } $targetAssoc = $targetClassMetadata->associationMappings[$fieldName]; foreach ($assoc['targetToSourceKeyColumns'] as $referencedColumn => $localColumn) { if (isset($targetAssoc['sourceToTargetKeyColumns'][$referencedColumn])) { $data[$localColumn] = $fieldValue; } } } continue; } if (!isset($assoc['id'])) { $targetClass = ClassUtils::getClass($data[$name]); $targetId = $this->uow->getEntityIdentifier($data[$name]); $data[$name] = new AssociationCacheEntry($targetClass, $targetId); continue; } // handle association identifier $targetId = is_object($data[$name]) && $this->uow->isInIdentityMap($data[$name]) ? $this->uow->getEntityIdentifier($data[$name]) : $data[$name]; // @TODO - fix it ! // handle UnitOfWork#createEntity hash generation if (!is_array($targetId)) { $data[reset($assoc['joinColumnFieldNames'])] = $targetId; $targetEntity = $this->em->getClassMetadata($assoc['targetEntity']); $targetId = [$targetEntity->identifier[0] => $targetId]; } $data[$name] = new AssociationCacheEntry($assoc['targetEntity'], $targetId); } return new EntityCacheEntry($metadata->name, $data); }
/** * Determines whether an entity instance is managed in this EntityManager. * * @param object $entity * * @return boolean TRUE if this EntityManager currently manages the given entity, FALSE otherwise. */ public function contains($entity) { return $this->unitOfWork->isScheduledForInsert($entity) || $this->unitOfWork->isInIdentityMap($entity) && !$this->unitOfWork->isScheduledForDelete($entity); }
/** * @param object $reference * @param UnitOfWork $uow * @return array */ protected function getIdentifier($reference, UnitOfWork $uow) { // In case Reference is not yet managed in UnitOfWork if (!$uow->isInIdentityMap($reference)) { $class = $this->manager->getClassMetadata(get_class($reference)); return $class->getIdentifierValues($reference); } // Dealing with ORM UnitOfWork if (method_exists($uow, 'getEntityIdentifier')) { return $uow->getEntityIdentifier($reference); } /** @todo handle this case */ }
/** * @param object $entity * @param UnitOfWork $uow * * @return string */ private function getIdString($entity, UnitOfWork $uow) { if ($uow->isInIdentityMap($entity)) { $ids = $uow->getEntityIdentifier($entity); $idstring = ""; foreach ($ids as $k => $v) { $idstring .= $k . "=" . $v; } } else { $idstring = "NEWOBJECT "; } $state = $uow->getEntityState($entity); if ($state == UnitOfWork::STATE_NEW) { $idstring .= " [NEW]"; } elseif ($state == UnitOfWork::STATE_REMOVED) { $idstring .= " [REMOVED]"; } elseif ($state == UnitOfWork::STATE_MANAGED) { $idstring .= " [MANAGED]"; } elseif ($state == UnitOfwork::STATE_DETACHED) { $idstring .= " [DETACHED]"; } return $idstring; }