. */ namespace Doctrine\ORM\Id; use Doctrine\ORM\EntityManager; use Doctrine\ORM\ORMException; /** * Special generator for application-assigned identifiers (doesn't really generate anything). * * @since 2.0 * @author Benjamin Eberlei * @author Guilherme Blanco * @author Jonathan Wage * @author Roman Borschel */ class AssignedGenerator extends AbstractIdGenerator { /** * Returns the identifier assigned to the given entity. * * @param EntityManager $em * @param object $entity * * @return mixed * * @throws \Doctrine\ORM\ORMException * * @override */ public function generate(EntityManager $em, $entity) { $class = $em->getClassMetadata(get_class($entity)); $idFields = $class->getIdentifierFieldNames(); $identifier = array(); foreach ($idFields as $idField) { $value = $class->getFieldValue($entity, $idField); if ( ! isset($value)) { throw ORMException::entityMissingAssignedIdForField($entity, $idField); } if (isset($class->associationMappings[$idField])) { if ( ! $em->getUnitOfWork()->isInIdentityMap($value)) { throw ORMException::entityMissingForeignAssignedId($entity, $value); } // NOTE: Single Columns as associated identifiers only allowed - this constraint it is enforced. $value = current($em->getUnitOfWork()->getEntityIdentifier($value)); } $identifier[$idField] = $value; } return $identifier; } }