/** * @param object $entity * @return int * @throws \InvalidArgumentException */ public function getEntityId($entity) { $className = $this->class; if (!$entity instanceof $className) { throw new \InvalidArgumentException('Expected instance of ' . $this->class); } $idFields = $this->metadata->getIdentifierFieldNames(); $idField = current($idFields); $entityIds = $this->metadata->getIdentifierValues($entity); return $entityIds[$idField]; }
/** * Returns the values of the identifier fields of an entity. * * Doctrine must know about this entity, that is, the entity must already * be persisted or added to the identity map before. Otherwise an * exception is thrown. * * @param object $entity The entity for which to get the identifier * * @return array The identifier values * * @throws FormException If the entity does not exist in Doctrine's identity map */ public function getIdentifierValues($entity) { if (!$this->em->contains($entity)) { throw new FormException('Entities passed to the choice field must be managed'); } return $this->classMetadata->getIdentifierValues($entity); }
/** * Returns the values of the identifier fields of an entity. * * Doctrine must know about this entity, that is, the entity must already * be persisted or added to the identity map before. Otherwise an * exception is thrown. * * @param object $entity The entity for which to get the identifier * * @return array The identifier values * * @throws Exception If the entity does not exist in Doctrine's identity map */ private function getIdentifierValues($entity) { if (!$this->em->contains($entity)) { throw new Exception('Entities passed to the choice field must be managed. Maybe ' . 'persist them in the entity manager?'); } $this->em->initializeObject($entity); return $this->classMetadata->getIdentifierValues($entity); }
/** * Returns the ID value for an object. * * This method assumes that the object has a single-column ID. * * @param object $object The object. * * @return mixed The ID value. */ public function getIdValue($object) { if (!$object) { return; } if (!$this->om->contains($object)) { throw new RuntimeException('Entities passed to the choice field must be managed. Maybe ' . 'persist them in the entity manager?'); } $this->om->initializeObject($object); return current($this->classMetadata->getIdentifierValues($object)); }
/** * @param mixed $valueOrObject * @param $target * @return array */ protected function toMany($valueOrObject, $target) { if (!is_array($valueOrObject)) { $valueOrObject = (array) $valueOrObject; } $values = array(); foreach ($valueOrObject as $value) { if (is_numeric($value)) { $values[] = $this->objectManager->find($target, $value); continue; } $identifiers = $this->metadata->getIdentifierValues($valueOrObject); $values[] = $this->objectManager->find($target, $identifiers); } return $values; }
/** * Creates a closure capable of finalizing state a cloned proxy * * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata * @param \Doctrine\ORM\Persisters\BasicEntityPersister $entityPersister * * @return \Closure * * @throws \Doctrine\ORM\EntityNotFoundException */ private function createCloner(ClassMetadata $classMetadata, BasicEntityPersister $entityPersister) { return function (BaseProxy $proxy) use($entityPersister, $classMetadata) { if ($proxy->__isInitialized()) { return; } $proxy->__setInitialized(true); $proxy->__setInitializer(null); $class = $entityPersister->getClassMetadata(); $original = $entityPersister->load($classMetadata->getIdentifierValues($proxy)); if (null === $original) { throw new EntityNotFoundException(); } foreach ($class->getReflectionClass()->getProperties() as $reflectionProperty) { $propertyName = $reflectionProperty->getName(); if ($class->hasField($propertyName) || $class->hasAssociation($propertyName)) { $reflectionProperty->setAccessible(true); $reflectionProperty->setValue($proxy, $reflectionProperty->getValue($original)); } } }; }
/** * Creates a closure capable of finalizing state a cloned proxy * * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $classMetadata * @param \Doctrine\ORM\Persisters\Entity\EntityPersister $entityPersister * * @return \Closure * * @throws \Doctrine\ORM\EntityNotFoundException */ private function createCloner(ClassMetadata $classMetadata, EntityPersister $entityPersister) { return function (BaseProxy $proxy) use($entityPersister, $classMetadata) { if ($proxy->__isInitialized()) { return; } $proxy->__setInitialized(true); $proxy->__setInitializer(null); $class = $entityPersister->getClassMetadata(); $identifier = $classMetadata->getIdentifierValues($proxy); $original = $entityPersister->loadById($identifier); if (null === $original) { throw EntityNotFoundException::fromClassNameAndIdentifier($classMetadata->getName(), $this->identifierFlattener->flattenIdentifier($classMetadata, $identifier)); } foreach ($class->getReflectionClass()->getProperties() as $property) { if (!$class->hasField($property->name) && !$class->hasAssociation($property->name)) { continue; } $property->setAccessible(true); $property->setValue($proxy, $property->getValue($original)); } }; }
/** * {@inheritdoc} */ public function modelName($model, ClassMetadata $metadata) { $identifiers = $metadata->getIdentifierValues($model); $className = lcfirst(ClassUtils::getClassName($metadata->getName())); return $className . implode('_', $identifiers); }
/** * @param ClassMetadata $metadata * @param object $object * * @return MissingBatchItemException */ public static function fromInvalidReference(ClassMetadata $metadata, $object) { return new self(sprintf('Requested batch item %s#%s (of type %s) with identifier "%s" could not be found', get_class($object), spl_object_hash($object), $metadata->getName(), json_encode($metadata->getIdentifierValues($object)))); }