/**
  * @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException
  */
 public function testClosureShouldReturnArray()
 {
     $closure = function () {
         return 'foobar';
     };
     $list = new ArrayChoiceList($closure);
     $list->getChoices();
 }
Esempio n. 2
0
 /**
  * Initializes the choices and returns them
  *
  * The choices are generated from the models. If the models have a
  * composite identifier, the choices are indexed using ascending integers.
  * Otherwise the identifiers are used as indices.
  *
  * If the models 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 models
  * 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()
 {
     parent::load();
     if ($this->choices) {
         $models = $this->choices;
     } else {
         $models = $this->query->find();
     }
     $this->choices = array();
     $this->models = array();
     foreach ($models as $key => $model) {
         if ($this->propertyPath) {
             // If the property option was given, use it
             $value = $this->propertyPath->getValue($model);
         } else {
             // Otherwise expect a __toString() method in the model
             $value = (string) $model;
         }
         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->models[$key] = $model;
         } else {
             // When the identifier is a single field, index choices by
             // model ID for performance reasons
             $id = current($this->getIdentifierValues($model));
             $this->choices[$id] = $value;
             $this->models[$id] = $model;
         }
     }
 }
 /**
  * {@inheritdoc}
  */
 public function getChoices()
 {
     $choices = parent::getChoices();
     $array = array();
     foreach ($choices as $value => $label) {
         $array[] = array('value' => $value, 'label' => $label);
     }
     return $array;
 }
 protected function load()
 {
     parent::load();
     if ($this->choices) {
         $documents = $this->choices;
     } elseif ($this->query) {
         $documents = $this->query->all();
     } else {
         $documents = $this->mandango->getRepository($this->class)->createQuery()->all();
     }
     $this->documents = $documents;
     $this->choices = array();
     foreach ($documents as $document) {
         if (null !== $this->field) {
             $value = $this->field;
         } elseif (method_exists($document, '__toString')) {
             $value = $document->__toString();
         } else {
             $value = $document->getId();
         }
         $this->choices[(string) $document->getId()] = $value;
     }
 }
Esempio n. 5
0
    /**
     * 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()
    {
        parent::load();

        if ($this->choices) {
            $entities = $this->choices;
        } else if ($qb = $this->queryBuilder) {
            $entities = $qb->getQuery()->execute();
        } else {
            $entities = $this->em->getRepository($this->class)->findAll();
        }

        $propertyPath = null;
        $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;
            }
        }
    }
Esempio n. 6
0
 /**
  * Initializes the choices and returns them
  *
  * 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.
  *
  * @return array  An array of choices
  */
 protected function load()
 {
     parent::load();
     if ($this->choices) {
         $entities = $this->choices;
     } else {
         if ($qb = $this->queryBuilder) {
             $entities = $qb->getQuery()->execute();
         } else {
             $entities = $this->em->getRepository($this->class)->findAll();
         }
     }
     $this->choices = array();
     $this->entities = array();
     $this->loadEntities($entities);
     return $this->choices;
 }
Esempio n. 7
0
 /**
  * Initializes the choices and returns them
  *
  * The choices are generated from the documents.
  *
  * If the documents 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 documents
  * 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()
 {
     parent::load();
     if ($this->choices) {
         $documents = $this->choices;
     } else {
         if ($queryBuilder = $this->queryBuilder) {
             $documents = $queryBuilder->getQuery()->execute();
         } else {
             $documents = $this->documentManager->getRepository($this->class)->findAll();
         }
     }
     $this->choices = array();
     $this->documents = array();
     foreach ($documents as $key => $document) {
         if ($this->propertyPath) {
             // If the property option was given, use it
             $value = $this->propertyPath->getValue($document);
         } else {
             // Otherwise expect a __toString() method in the document
             $value = $document->__toString();
         }
         $id = $this->getIdentifierValue($document);
         $this->choices[$id] = $value;
         $this->documents[$id] = $document;
     }
 }
Esempio n. 8
0
 /**
  * Initializes the choices and returns them.
  *
  * 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.
  *
  * @return array  An array of choices
  */
 protected function load()
 {
     parent::load();
     if (is_array($this->choices)) {
         $entities = $this->choices;
     } else {
         if ($entityLoader = $this->entityLoader) {
             $entities = $entityLoader->getEntities();
         } else {
             $entities = $this->em->getRepository($this->class)->findAll();
         }
     }
     $this->choices = array();
     $this->entities = array();
     if ($this->groupBy) {
         $entities = $this->groupEntities($entities, $this->groupBy);
     }
     $this->loadEntities($entities);
     return $this->choices;
 }