[DoctrineBridge] Remove large parts of the EntityChoiceList code that were completly unnecessary (code was unreachable).
This commit is contained in:
parent
b919d92b52
commit
3b5c617ad0
@ -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,27 +281,11 @@ 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];
|
||||
}
|
||||
}
|
||||
} 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;
|
||||
}
|
||||
|
@ -18,15 +18,6 @@ namespace Symfony\Bridge\Doctrine\Form\ChoiceList;
|
||||
*/
|
||||
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 @@ 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();
|
||||
}
|
||||
}
|
@ -22,6 +22,6 @@ class SingleIdentEntity
|
||||
|
||||
public function __toString()
|
||||
{
|
||||
return (string)$this->id;
|
||||
return (string)$this->name;
|
||||
}
|
||||
}
|
||||
|
@ -112,6 +112,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
|
||||
*/
|
||||
|
Reference in New Issue
Block a user