/**
  * Initializes the choices and returns them
  *
  * The choices are generated from the entities. If the entities have a
  * composite identifier, the choices are indexed using ascending integers.
  * Otherwise the identifiers are used as indices.
  *
  * If the entities were passed in the "choices" option, this method
  * does not have any significant overhead. Otherwise, if a query builder
  * was passed in the "query_builder" option, this builder is now used
  * to construct a query which is executed. In the last case, all entities
  * for the underlying class are fetched from the repository.
  *
  * If the option "property" was passed, the property path in that option
  * is used as option values. Otherwise this method tries to convert
  * objects to strings using __toString().
  *
  * @return array  An array of choices
  */
 protected function load()
 {
     if (is_array($this->choices)) {
         $entities = $this->choices;
     } else {
         if ($this->query) {
             $entities = $this->modelManager->executeQuery($this->query);
         } else {
             $entities = $this->modelManager->findBy($this->class);
         }
     }
     $this->choices = array();
     $this->entities = array();
     foreach ($entities as $key => $entity) {
         if ($this->propertyPath) {
             // If the property option was given, use it
             $value = $this->propertyPath->getValue($entity);
         } else {
             // Otherwise expect a __toString() method in the entity
             $value = (string) $entity;
         }
         if (count($this->identifier) > 1) {
             // When the identifier consists of multiple field, use
             // naturally ordered keys to refer to the choices
             $this->choices[$key] = $value;
             $this->entities[$key] = $entity;
         } else {
             // When the identifier is a single field, index choices by
             // entity ID for performance reasons
             $id = current($this->getIdentifierValues($entity));
             $this->choices[$id] = $value;
             $this->entities[$id] = $entity;
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function loadChoiceList($value = null)
 {
     if (!$this->choiceList) {
         if ($this->query) {
             $entities = $this->modelManager->executeQuery($this->query);
         } elseif (is_array($this->choices) && count($this->choices) > 0) {
             $entities = $this->choices;
         } else {
             $entities = $this->modelManager->findBy($this->class);
         }
         $choices = array();
         foreach ($entities as $key => $entity) {
             if ($this->propertyPath) {
                 // If the property option was given, use it
                 $propertyAccessor = PropertyAccess::createPropertyAccessor();
                 $valueObject = $propertyAccessor->getValue($entity, $this->propertyPath);
             } else {
                 // Otherwise expect a __toString() method in the entity
                 try {
                     $valueObject = (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);
                 }
             }
             $id = implode('~', $this->getIdentifierValues($entity));
             if (!array_key_exists($valueObject, $choices)) {
                 $choices[$valueObject] = array();
             }
             $choices[$valueObject][] = $id;
         }
         $finalChoices = array();
         foreach ($choices as $valueObject => $idx) {
             if (count($idx) > 1) {
                 // avoid issue with identical values ...
                 foreach ($idx as $id) {
                     $finalChoices[sprintf('%s (id: %s)', $valueObject, $id)] = $id;
                 }
             } else {
                 $finalChoices[$valueObject] = current($idx);
             }
         }
         $this->choiceList = new ArrayChoiceList($finalChoices, $value);
     }
     return $this->choiceList;
 }
 /**
  * Initializes the choices and returns them.
  *
  * The choices are generated from the entities. If the entities have a
  * composite identifier, the choices are indexed using ascending integers.
  * Otherwise the identifiers are used as indices.
  *
  * If the entities were passed in the "choices" option, this method
  * does not have any significant overhead. Otherwise, if a query builder
  * was passed in the "query" option, this builder is now used to construct
  * a query which is executed. In the last case, all entities for the
  * underlying class are fetched from the repository.
  *
  * If the option "property" was passed, the property path in that option
  * is used as option values. Otherwise this method tries to convert
  * objects to strings using __toString().
  *
  * @param $choices
  *
  * @return array An array of choices
  */
 protected function load($choices)
 {
     if (is_array($choices) && count($choices) > 0) {
         $entities = $choices;
     } elseif ($this->query) {
         $entities = $this->modelManager->executeQuery($this->query);
     } else {
         $entities = $this->modelManager->findBy($this->class);
     }
     if (null === $entities) {
         return array();
     }
     $choices = array();
     $this->entities = array();
     foreach ($entities as $key => $entity) {
         if ($this->propertyPath) {
             // If the property option was given, use it
             $propertyAccessor = PropertyAccess::createPropertyAccessor();
             $value = $propertyAccessor->getValue($entity, $this->propertyPath);
         } else {
             // Otherwise expect a __toString() method in the entity
             try {
                 $value = (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);
             }
         }
         if (count($this->identifier) > 1) {
             // When the identifier consists of multiple field, use
             // naturally ordered keys to refer to the choices
             $choices[$key] = $value;
             $this->entities[$key] = $entity;
         } else {
             // When the identifier is a single field, index choices by
             // entity ID for performance reasons
             $id = current($this->getIdentifierValues($entity));
             $choices[$id] = $value;
             $this->entities[$id] = $entity;
         }
     }
     return $choices;
 }