/** * {@inheritdoc} */ public function reverseTransform($keys) { $collection = $this->choiceList->getModelManager()->getModelCollectionInstance($this->choiceList->getClass()); if (!$collection instanceof \ArrayAccess) { throw new UnexpectedTypeException($collection, '\\ArrayAccess'); } if ('' === $keys || null === $keys) { return $collection; } if (!is_array($keys)) { throw new UnexpectedTypeException($keys, 'array'); } $notFound = array(); // optimize this into a SELECT WHERE IN query foreach ($keys as $key) { if ($entity = $this->choiceList->getEntity($key)) { $collection[] = $entity; } else { $notFound[] = $key; } } if (count($notFound) > 0) { throw new TransformationFailedException(sprintf('The entities with keys "%s" could not be found', implode('", "', $notFound))); } return $collection; }
public function testLoadArrayOfChoices() { // Get choices from Array of choices, count($this->identifier) > 1 $result = array(1, 2); $modelChoice = new ModelChoiceList($this->modelManager, 'Sonata\\AdminBundle\\Tests\\Fixtures\\Entity\\Foo', null, null, $result); $this->assertSame(array_keys($result), $modelChoice->getChoices()); }