Permalink
Browse files

[DoctrineBridge] Remove large parts of the EntityChoiceList code that…

… were completly unnecessary (code was unreachable).
  • Loading branch information...
1 parent b919d92 commit 3b5c617ad01e36f74bd949d55d90326c69acc85e @beberlei beberlei committed with stof Nov 26, 2011
@@ -273,34 +273,18 @@ public function getEntities()
* identifiers)
* @return object[] The matching entity
*/
- public function getEntitiesByKeys($keys)
+ public function getEntitiesByKeys(array $keys)
{
if (!$this->loaded) {
$this->load();
}
$found = array();
- if (count($this->identifier) > 1) {
- // $key is a collection index
- $entities = $this->getEntities();
- foreach ($keys as $key) {
- if (isset($entities[$key])) {
- $found[] = $entities[$key];
- }
+ foreach ($keys as $key) {
+ if (isset($this->entities[$key])) {
+ $found[] = $this->entities[$key];
}
- } else if ($this->entities) {
- foreach ($keys as $key) {
- if (isset($this->entities[$key])) {
- $found[] = $this->entities[$key];
- }
- }
- } else if ($entityLoader = $this->entityLoader) {
- $found = $entityLoader->getEntitiesByKeys($this->identifier, $keys);
- } else if ($keys) {
- $identifier = current($this->identifier);
- $found = $this->em->getRepository($this->class)
- ->findBy(array($identifier => $keys));
}
return $found;
@@ -17,16 +17,7 @@
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
interface EntityLoaderInterface
-{
- /**
- * Given choice list values this method returns the appropriate entities for it.
- *
- * @param array $identifier
- * @param array $choiceListKeys Array of values of the select option, checkbox or radio button.
- * @return object[]
- */
- function getEntitiesByKeys(array $identifier, array $choiceListKeys);
-
+{
/**
* Return an array of entities that are valid choices in the corresponding choice list.
*
@@ -64,23 +64,4 @@ public function getEntities()
{
return $this->queryBuilder->getQuery()->execute();
}
-
- /**
- * {@inheritDoc}
- */
- public function getEntitiesByKeys(array $identifier, array $choiceListKeys)
- {
- if (count($identifier) != 1) {
- throw new FormException("Only entities with one identifier supported by ORM QueryBuilder.");
- }
-
- $qb = clone ($this->queryBuilder);
- $alias = $qb->getRootAlias();
- $where = $qb->expr()->in($alias.'.'.current($identifier), "?1");
-
- return $qb->andWhere($where)
- ->getQuery()
- ->setParameter(1, $choiceListKeys, Connection::PARAM_STR_ARRAY)
- ->getResult();
- }
}
@@ -22,6 +22,6 @@ public function __construct($id, $name) {
public function __toString()
{
- return (string)$this->id;
+ return (string)$this->name;
}
}
@@ -111,6 +111,41 @@ public function testSetDataToUninitializedEntityWithNonRequired()
$this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->createView()->get('choices'));
}
+
+ public function testSetDataToUninitializedEntityWithNonRequiredToString()
+ {
+ $entity1 = new SingleIdentEntity(1, 'Foo');
+ $entity2 = new SingleIdentEntity(2, 'Bar');
+
+ $this->persist(array($entity1, $entity2));
+
+ $field = $this->factory->createNamed('entity', 'name', null, array(
+ 'em' => 'default',
+ 'class' => self::SINGLE_IDENT_CLASS,
+ 'required' => false,
+ ));
+
+ $this->assertEquals(array("1" => 'Foo', "2" => 'Bar'), $field->createView()->get('choices'));
+ }
+
+ public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()
+ {
+ $entity1 = new SingleIdentEntity(1, 'Foo');
+ $entity2 = new SingleIdentEntity(2, 'Bar');
+
+ $this->persist(array($entity1, $entity2));
+ $qb = $this->em->createQueryBuilder()->select('e')->from(self::SINGLE_IDENT_CLASS, 'e');
+
+ $field = $this->factory->createNamed('entity', 'name', null, array(
+ 'em' => 'default',
+ 'class' => self::SINGLE_IDENT_CLASS,
+ 'required' => false,
+ 'property' => 'name',
+ 'query_builder' => $qb
+ ));
+
+ $this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->createView()->get('choices'));
+ }
/**
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException

0 comments on commit 3b5c617

Please sign in to comment.