[DoctrineBridge] Remove large parts of the EntityChoiceList code that were completly unnecessary (code was unreachable).

This commit is contained in:
Benjamin Eberlei 2011-11-26 18:21:26 +01:00 committed by Christophe Coevoet
parent b919d92b52
commit 3b5c617ad0
5 changed files with 41 additions and 50 deletions

View File

@ -273,7 +273,7 @@ class EntityChoiceList extends ArrayChoiceList
* identifiers)
* @return object[] The matching entity
*/
public function getEntitiesByKeys($keys)
public function getEntitiesByKeys(array $keys)
{
if (!$this->loaded) {
$this->load();
@ -281,26 +281,10 @@ class EntityChoiceList extends ArrayChoiceList
$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;

View File

@ -17,16 +17,7 @@ namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
* @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.
*

View File

@ -64,23 +64,4 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
{
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();
}
}

View File

@ -22,6 +22,6 @@ class SingleIdentEntity
public function __toString()
{
return (string)$this->id;
return (string)$this->name;
}
}

View File

@ -111,6 +111,41 @@ class EntityTypeTest extends TypeTestCase
$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