/** * Returns the model objects corresponding to the given values. * * @param array $values * * @return array * * @see Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface */ public function getChoicesForValues(array $values) { if (!$this->loaded) { if (1 === count($this->identifier)) { $filterBy = 'filterBy' . current($this->identifier)->getPhpName(); return (array) $this->query->create()->{$filterBy}($values)->find(); } $this->load(); } return parent::getChoicesForValues($values); }
/** * {@inheritdoc} */ public function getChoicesForValues(array $values) { if (empty($values)) { return array(); } /** * This performance optimization reflects a common scenario: * * A simple select of a model entry. * * The choice option "expanded" is set to false. * * The current request is the submission of the selected value. * * @see \Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer::reverseTransform * @see \Symfony\Component\Form\Extension\Core\DataTransformer\ChoiceToValueTransformer::reverseTransform */ if (!$this->loaded) { if (1 === count($this->identifier)) { $filterBy = 'filterBy' . current($this->identifier)->getPhpName(); // The initial query is cloned, so all additional filters are applied correctly. $query = clone $this->query; $result = (array) $query->{$filterBy}($values)->find(); // Preserve the keys as provided by the values. $models = array(); foreach ($values as $index => $value) { foreach ($result as $eachModel) { if ($value === $this->createValue($eachModel)) { // Make sure to convert to the right format $models[$index] = $this->fixChoice($eachModel); // If all values have been assigned, skip further loops. unset($values[$index]); if (0 === count($values)) { break 2; } } } } return $models; } } $this->load(); return parent::getChoicesForValues($values); }
/** * Returns the entities corresponding to the given values. * * @param array $values * * @return array * * @see Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface */ public function getChoicesForValues(array $values) { if (!$this->loaded) { // Optimize performance in case we have an entity loader and // a single-field identifier if (count($this->identifier) === 1 && $this->entityLoader) { if (empty($values)) { return array(); } return $this->entityLoader->getEntitiesByIds(current($this->identifier), $values); } $this->load(); } return parent::getChoicesForValues($values); }
/** * Returns the entities corresponding to the given values. * * @param array $values * * @return array * * @see Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface */ public function getChoicesForValues(array $values) { // Performance optimization // Also prevents the generation of "WHERE id IN ()" queries through the // entity loader. At least with MySQL and on the development machine // this was tested on, no exception was thrown for such invalid // statements, consequently no test fails when this code is removed. // https://github.com/symfony/symfony/pull/8981#issuecomment-24230557 if (empty($values)) { return array(); } if (!$this->loaded) { // Optimize performance in case we have an entity loader and // a single-field identifier if ($this->idAsValue && $this->entityLoader) { $unorderedEntities = $this->entityLoader->getEntitiesByIds($this->idField, $values); $entitiesByValue = array(); $entities = array(); // Maintain order and indices from the given $values // An alternative approach to the following loop is to add the // "INDEX BY" clause to the Doctrine query in the loader, // but I'm not sure whether that's doable in a generic fashion. foreach ($unorderedEntities as $entity) { $value = $this->fixValue(current($this->getIdentifierValues($entity))); $entitiesByValue[$value] = $entity; } foreach ($values as $i => $value) { if (isset($entitiesByValue[$value])) { $entities[$i] = $entitiesByValue[$value]; } } return $entities; } $this->load(); } return parent::getChoicesForValues($values); }