Skip to content

Commit 3b5c617

Browse files
beberleistof
authored andcommitted
[DoctrineBridge] Remove large parts of the EntityChoiceList code that were completly unnecessary (code was unreachable).
1 parent b919d92 commit 3b5c617

File tree

5 files changed

+41
-50
lines changed

5 files changed

+41
-50
lines changed

src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -273,34 +273,18 @@ public function getEntities()
273273
* identifiers)
274274
* @return object[] The matching entity
275275
*/
276-
public function getEntitiesByKeys($keys)
276+
public function getEntitiesByKeys(array $keys)
277277
{
278278
if (!$this->loaded) {
279279
$this->load();
280280
}
281281

282282
$found = array();
283283

284-
if (count($this->identifier) > 1) {
285-
// $key is a collection index
286-
$entities = $this->getEntities();
287-
foreach ($keys as $key) {
288-
if (isset($entities[$key])) {
289-
$found[] = $entities[$key];
290-
}
284+
foreach ($keys as $key) {
285+
if (isset($this->entities[$key])) {
286+
$found[] = $this->entities[$key];
291287
}
292-
} else if ($this->entities) {
293-
foreach ($keys as $key) {
294-
if (isset($this->entities[$key])) {
295-
$found[] = $this->entities[$key];
296-
}
297-
}
298-
} else if ($entityLoader = $this->entityLoader) {
299-
$found = $entityLoader->getEntitiesByKeys($this->identifier, $keys);
300-
} else if ($keys) {
301-
$identifier = current($this->identifier);
302-
$found = $this->em->getRepository($this->class)
303-
->findBy(array($identifier => $keys));
304288
}
305289

306290
return $found;

src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityLoaderInterface.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,7 @@
1717
* @author Benjamin Eberlei <kontakt@beberlei.de>
1818
*/
1919
interface EntityLoaderInterface
20-
{
21-
/**
22-
* Given choice list values this method returns the appropriate entities for it.
23-
*
24-
* @param array $identifier
25-
* @param array $choiceListKeys Array of values of the select option, checkbox or radio button.
26-
* @return object[]
27-
*/
28-
function getEntitiesByKeys(array $identifier, array $choiceListKeys);
29-
20+
{
3021
/**
3122
* Return an array of entities that are valid choices in the corresponding choice list.
3223
*

src/Symfony/Bridge/Doctrine/Form/ChoiceList/ORMQueryBuilderLoader.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,23 +64,4 @@ public function getEntities()
6464
{
6565
return $this->queryBuilder->getQuery()->execute();
6666
}
67-
68-
/**
69-
* {@inheritDoc}
70-
*/
71-
public function getEntitiesByKeys(array $identifier, array $choiceListKeys)
72-
{
73-
if (count($identifier) != 1) {
74-
throw new FormException("Only entities with one identifier supported by ORM QueryBuilder.");
75-
}
76-
77-
$qb = clone ($this->queryBuilder);
78-
$alias = $qb->getRootAlias();
79-
$where = $qb->expr()->in($alias.'.'.current($identifier), "?1");
80-
81-
return $qb->andWhere($where)
82-
->getQuery()
83-
->setParameter(1, $choiceListKeys, Connection::PARAM_STR_ARRAY)
84-
->getResult();
85-
}
8667
}

tests/Symfony/Tests/Bridge/Doctrine/Fixtures/SingleIdentEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ public function __construct($id, $name) {
2222

2323
public function __toString()
2424
{
25-
return (string)$this->id;
25+
return (string)$this->name;
2626
}
2727
}

tests/Symfony/Tests/Bridge/Doctrine/Form/Type/EntityTypeTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,41 @@ public function testSetDataToUninitializedEntityWithNonRequired()
111111

112112
$this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->createView()->get('choices'));
113113
}
114+
115+
public function testSetDataToUninitializedEntityWithNonRequiredToString()
116+
{
117+
$entity1 = new SingleIdentEntity(1, 'Foo');
118+
$entity2 = new SingleIdentEntity(2, 'Bar');
119+
120+
$this->persist(array($entity1, $entity2));
121+
122+
$field = $this->factory->createNamed('entity', 'name', null, array(
123+
'em' => 'default',
124+
'class' => self::SINGLE_IDENT_CLASS,
125+
'required' => false,
126+
));
127+
128+
$this->assertEquals(array("1" => 'Foo', "2" => 'Bar'), $field->createView()->get('choices'));
129+
}
130+
131+
public function testSetDataToUninitializedEntityWithNonRequiredQueryBuilder()
132+
{
133+
$entity1 = new SingleIdentEntity(1, 'Foo');
134+
$entity2 = new SingleIdentEntity(2, 'Bar');
135+
136+
$this->persist(array($entity1, $entity2));
137+
$qb = $this->em->createQueryBuilder()->select('e')->from(self::SINGLE_IDENT_CLASS, 'e');
138+
139+
$field = $this->factory->createNamed('entity', 'name', null, array(
140+
'em' => 'default',
141+
'class' => self::SINGLE_IDENT_CLASS,
142+
'required' => false,
143+
'property' => 'name',
144+
'query_builder' => $qb
145+
));
146+
147+
$this->assertEquals(array(1 => 'Foo', 2 => 'Bar'), $field->createView()->get('choices'));
148+
}
114149

115150
/**
116151
* @expectedException Symfony\Component\Form\Exception\UnexpectedTypeException

0 commit comments

Comments
 (0)