Skip to content

Commit

Permalink
[DoctrineBridge] Remove large parts of the EntityChoiceList code that…
Browse files Browse the repository at this point in the history
… were completly unnecessary (code was unreachable).
  • Loading branch information
beberlei authored and stof committed Dec 19, 2011
1 parent b919d92 commit 3b5c617
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 50 deletions.
24 changes: 4 additions & 20 deletions src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
Expand Up @@ -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;
Expand Down
Expand Up @@ -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.
*
Expand Down
Expand Up @@ -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();
}
}
Expand Up @@ -22,6 +22,6 @@ public function __construct($id, $name) {

public function __toString()
{
return (string)$this->id;
return (string)$this->name;
}
}
35 changes: 35 additions & 0 deletions tests/Symfony/Tests/Bridge/Doctrine/Form/Type/EntityTypeTest.php
Expand Up @@ -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
Expand Down

0 comments on commit 3b5c617

Please sign in to comment.