/**
  * {@inheritdoc}
  */
 public function getValuesForChoices(array $choices)
 {
     if (!$this->valuePath) {
         return parent::getValuesForChoices($choices);
     }
     // Use the value path to compare the choices
     $choices = $this->fixChoices($choices);
     $values = array();
     foreach ($choices as $i => $givenChoice) {
         // Ignore non-readable choices
         if (!is_object($givenChoice) && !is_array($givenChoice)) {
             continue;
         }
         $givenValue = (string) $this->propertyAccessor->getValue($givenChoice, $this->valuePath);
         foreach ($this->values as $value) {
             if ($value === $givenValue) {
                 $values[$i] = $value;
                 unset($choices[$i]);
                 if (0 === count($choices)) {
                     break 2;
                 }
             }
         }
     }
     return $values;
 }
示例#2
0
 /**
  * Returns the values corresponding to the given entities.
  *
  * @param array $entities
  *
  * @return array
  *
  * @see Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface
  */
 public function getValuesForChoices(array $entities)
 {
     if (!$this->loaded) {
         // Optimize performance for single-field identifiers. We already
         // know that the IDs are used as values
         // Attention: This optimization does not check choices for existence
         if ($this->idAsValue) {
             $values = array();
             foreach ($entities as $entity) {
                 if ($entity instanceof $this->class) {
                     // Make sure to convert to the right format
                     $values[] = $this->fixValue(current($this->getIdentifierValues($entity)));
                 }
             }
             return $values;
         }
         $this->load();
     }
     return parent::getValuesForChoices($entities);
 }