/**
  * 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
  *
  * @throws InvalidArgumentException If the entity does not exist in Doctrine's
  *                                  identity map
  *
  * @return array
  */
 public function getIdentifierValues($entity)
 {
     try {
         return $this->modelManager->getIdentifierValues($entity);
     } catch (\Exception $e) {
         throw new InvalidArgumentException(sprintf('Unable to retrieve the identifier values for entity %s', ClassUtils::getClass($entity)), 0, $e);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function transform($entityOrCollection)
 {
     $result = array();
     if (!$entityOrCollection) {
         return $result;
     }
     if ($this->multiple) {
         $isArray = is_array($entityOrCollection);
         if (!$isArray && substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
             throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
         } elseif ($isArray || $entityOrCollection instanceof \ArrayAccess) {
             $collection = $entityOrCollection;
         } else {
             throw new \InvalidArgumentException('A multiple selection must be passed a collection not a single value. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
         }
     } else {
         if (substr(get_class($entityOrCollection), -1 * strlen($this->className)) == $this->className) {
             $collection = array($entityOrCollection);
         } elseif ($entityOrCollection instanceof \ArrayAccess) {
             throw new \InvalidArgumentException('A single selection must be passed a single value not a collection. Make sure that form option "multiple=false" is set for many-to-one relation and "multiple=true" is set for many-to-many or one-to-many relations.');
         } else {
             $collection = array($entityOrCollection);
         }
     }
     if (empty($this->property)) {
         throw new \RuntimeException('Please define "property" parameter.');
     }
     foreach ($collection as $entity) {
         $id = current($this->modelManager->getIdentifierValues($entity));
         if ($this->toStringCallback !== null) {
             if (!is_callable($this->toStringCallback)) {
                 throw new \RuntimeException('Callback in "to_string_callback" option doesn`t contain callable function.');
             }
             $label = call_user_func($this->toStringCallback, $entity, $this->property);
         } else {
             try {
                 $label = (string) $entity;
             } catch (\Exception $e) {
                 throw new \RuntimeException(sprintf("Unable to convert the entity %s to String, entity must have a '__toString()' method defined", ClassUtils::getClass($entity)), 0, $e);
             }
         }
         $result[] = $id;
         $result['_labels'][] = $label;
     }
     return $result;
 }
Ejemplo n.º 3
0
 /**
  * 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
  * @throws FormException   If the entity does not exist in Doctrine's
  *                         identity map
  */
 public function getIdentifierValues($entity)
 {
     return $this->modelManager->getIdentifierValues($entity);
 }