Assigned.php 1.34 KB
Newer Older
romanb's avatar
romanb committed
1 2 3 4 5 6
<?php

/**
 * Special generator for application-assigned identifiers (doesnt really generate anything).
 *
 * @since 2.0
7
 * @author Roman Borschel <roman@code-factory.org>
romanb's avatar
romanb committed
8
 */
romanb's avatar
romanb committed
9
class Doctrine_ORM_Id_Assigned extends Doctrine_ORM_Id_AbstractIdGenerator
romanb's avatar
romanb committed
10 11
{
    /**
12
     * Returns the identifier assigned to the given entity.
romanb's avatar
romanb committed
13
     *
14 15
     * @param Doctrine\ORM\Entity $entity
     * @return mixed
romanb's avatar
romanb committed
16 17
     * @override
     */
18
    public function generate($entity)
romanb's avatar
romanb committed
19
    {
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
        $class = $this->_em->getClassMetadata(get_class($entity));
        if ($class->isIdentifierComposite()) {
            $identifier = array();
            $idFields = $class->getIdentifierFieldNames();
            foreach ($idFields as $idField) {
                $identifier[] =
                $value = $class->getReflectionProperty($idField)->getValue($entity);
                if (isset($value)) {
                    $identifier[] = $value;
                }
            }
        } else {
            $value = $class->getReflectionProperty($class->getSingleIdentifierFieldName())
                    ->getValue($entity);
            if (isset($value)) {
                $identifier = array($value);
            }
romanb's avatar
romanb committed
37
        }
38 39 40 41 42 43

        if ( ! $identifier) {
            throw new Doctrine_Exception("Entity '$entity' is missing an assigned ID.");
        }
        
        return $identifier;
romanb's avatar
romanb committed
44 45 46
    }
}