Skip to content

Commit

Permalink
[DoctrineBridge] Extracted the common type and made the choice list g…
Browse files Browse the repository at this point in the history
…eneric
  • Loading branch information
stof committed Dec 19, 2011
1 parent 3c81b62 commit 200ed54
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 86 deletions.
32 changes: 14 additions & 18 deletions src/Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php
Expand Up @@ -16,21 +16,24 @@
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\ChoiceList\ArrayChoiceList;
use Doctrine\Common\Persistence\ObjectManager;
use Doctrine\ORM\QueryBuilder;
use Doctrine\ORM\NoResultException;

class EntityChoiceList extends ArrayChoiceList
{
/**
* @var Doctrine\ORM\EntityManager
* @var ObjectManager
*/
private $em;

/**
* @var Doctrine\ORM\Mapping\ClassMetadata
* @var string
*/
private $class;

/**
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadata
*/
private $classMetadata;

/**
* The entities from which the user can choose
*
Expand All @@ -40,7 +43,7 @@ class EntityChoiceList extends ArrayChoiceList
* This property is initialized by initializeChoices(). It should only
* be accessed through getEntity() and getEntities().
*
* @var Collection
* @var array
*/
private $entities = array();

Expand All @@ -50,7 +53,7 @@ class EntityChoiceList extends ArrayChoiceList
*
* This property should only be accessed through queryBuilder.
*
* @var Doctrine\ORM\QueryBuilder
* @var EntityLoaderInterface
*/
private $entityLoader;

Expand All @@ -63,13 +66,6 @@ class EntityChoiceList extends ArrayChoiceList
*/
private $identifier = array();

/**
* A cache for the UnitOfWork instance of Doctrine
*
* @var Doctrine\ORM\UnitOfWork
*/
private $unitOfWork;

/**
* Property path to access the key value of this choice-list.
*
Expand Down Expand Up @@ -99,15 +95,15 @@ public function __construct(ObjectManager $manager, $class, $property = null, En
$this->em = $manager;
$this->class = $class;
$this->entityLoader = $entityLoader;
$this->unitOfWork = $manager->getUnitOfWork();
$this->identifier = $manager->getClassMetadata($class)->getIdentifierFieldNames();
$this->classMetadata = $manager->getClassMetadata($class);
$this->identifier = $this->classMetadata->getIdentifierFieldNames();
$this->groupBy = $groupBy;

// The property option defines, which property (path) is used for
// displaying entities as strings
if ($property) {
$this->propertyPath = new PropertyPath($property);
} else if (!method_exists($this->class, '__toString')) {
} elseif (!method_exists($this->class, '__toString')) {
// Otherwise expect a __toString() method in the entity
throw new FormException('Entities passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).');
}
Expand Down Expand Up @@ -305,10 +301,10 @@ public function getEntitiesByKeys(array $keys)
*/
public function getIdentifierValues($entity)
{
if (!$this->unitOfWork->isInIdentityMap($entity)) {
if (!$this->em->contains($entity)) {
throw new FormException('Entities passed to the choice field must be managed');
}

return $this->unitOfWork->getEntityIdentifier($entity);
return $this->classMetadata->getIdentifierValues($entity);
}
}
94 changes: 94 additions & 0 deletions src/Symfony/Bridge/Doctrine/Form/Type/DoctrineType.php
@@ -0,0 +1,94 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Doctrine\Form\Type;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormBuilder;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\EventListener\MergeCollectionListener;
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntitiesToArrayTransformer;
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntityToIdTransformer;
use Symfony\Component\Form\AbstractType;

abstract class DoctrineType extends AbstractType
{
/**
* @var ManagerRegistry
*/
protected $registry;

public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}

public function buildForm(FormBuilder $builder, array $options)
{
if ($options['multiple']) {
$builder
->addEventSubscriber(new MergeCollectionListener())
->prependClientTransformer(new EntitiesToArrayTransformer($options['choice_list']))
;
} else {
$builder->prependClientTransformer(new EntityToIdTransformer($options['choice_list']));
}
}

public function getDefaultOptions(array $options)
{
$defaultOptions = array(
'em' => null,
'class' => null,
'property' => null,
'query_builder' => null,
'loader' => null,
'choices' => null,
'group_by' => null,
);

$options = array_replace($defaultOptions, $options);

if (!isset($options['choice_list'])) {
$manager = $this->registry->getManager($options['em']);
if (isset($options['query_builder'])) {
$options['loader'] = $this->getLoader($manager, $options);
}

$defaultOptions['choice_list'] = new EntityChoiceList(
$manager,
$options['class'],
$options['property'],
$options['loader'],
$options['choices'],
$options['group_by']
);
}

return $defaultOptions;
}

/**
* Return the default loader object.
*
* @param ObjectManager $manager
* @param array $options
* @return EntityLoaderInterface
*/
abstract protected function getLoader(ObjectManager $manager, array $options);

public function getParent(array $options)
{
return 'choice';
}
}
73 changes: 5 additions & 68 deletions src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php
Expand Up @@ -11,80 +11,22 @@

namespace Symfony\Bridge\Doctrine\Form\Type;

use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\FormBuilder;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
use Symfony\Bridge\Doctrine\Form\EventListener\MergeCollectionListener;
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntitiesToArrayTransformer;
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntityToIdTransformer;
use Symfony\Component\Form\AbstractType;

class EntityType extends AbstractType
class EntityType extends DoctrineType
{
/**
* @var ManagerRegistry
*/
protected $registry;

public function __construct(ManagerRegistry $registry)
{
$this->registry = $registry;
}

public function buildForm(FormBuilder $builder, array $options)
{
if ($options['multiple']) {
$builder
->addEventSubscriber(new MergeCollectionListener())
->prependClientTransformer(new EntitiesToArrayTransformer($options['choice_list']))
;
} else {
$builder->prependClientTransformer(new EntityToIdTransformer($options['choice_list']));
}
}

public function getDefaultOptions(array $options)
{
$defaultOptions = array(
'em' => null,
'class' => null,
'property' => null,
'query_builder' => null,
'loader' => null,
'choices' => null,
'group_by' => null,
);

$options = array_replace($defaultOptions, $options);

if (!isset($options['choice_list'])) {
$manager = $this->registry->getManager($options['em']);
if (isset($options['query_builder'])) {
$options['loader'] = $this->getLoader($manager, $options);
}

$defaultOptions['choice_list'] = new EntityChoiceList(
$manager,
$options['class'],
$options['property'],
$options['loader'],
$options['choices'],
$options['group_by']
);
}

return $defaultOptions;
}

/**
* Return the default loader object.
*
* @param ObjectManager
* @param ObjectManager $manager
* @param array $options
* @return ORMQueryBuilderLoader
*/
protected function getLoader($manager, $options)
protected function getLoader(ObjectManager $manager, array $options)
{
return new ORMQueryBuilderLoader(
$options['query_builder'],
Expand All @@ -93,11 +35,6 @@ protected function getLoader($manager, $options)
);
}

public function getParent(array $options)
{
return 'choice';
}

public function getName()
{
return 'entity';
Expand Down

0 comments on commit 200ed54

Please sign in to comment.