8000 [DoctrineBridge] Extracted the common type and made the choice list g… · symfony/symfony@200ed54 · GitHub
[go: up one dir, main page]

Skip to content

Commit 200ed54

Browse files
committed
[DoctrineBridge] Extracted the common type and made the choice list generic
1 parent 3c81b62 commit 200ed54

File tree

3 files changed

+113
-86
lines changed

3 files changed

+113
-86
lines changed

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@
1616
use Symfony\Component\Form\Exception\UnexpectedTypeException;
1717
use Symfony\Component\Form\Extension\Core\ChoiceList\ArrayChoiceList;
1818
use Doctrine\Common\Persistence\ObjectManager;
19-
use Doctrine\ORM\QueryBuilder;
20-
use Doctrine\ORM\NoResultException;
2119

2220
class EntityChoiceList extends ArrayChoiceList
2321
{
2422
/**
25-
* @var Doctrine\ORM\EntityManager
23+
* @var ObjectManager
2624
*/
2725
private $em;
2826

2927
/**
30-
* @var Doctrine\ORM\Mapping\ClassMetadata
28+
* @var string
3129
*/
3230
private $class;
3331

32+
/**
33+
* @var \Doctrine\Common\Persistence\Mapping\ClassMetadata
34+
*/
35+
private $classMetadata;
36+
3437
/**
3538
* The entities from which the user can choose
3639
*
@@ -40,7 +43,7 @@ class EntityChoiceList extends ArrayChoiceList
4043
* This property is initialized by initializeChoices(). It should only
4144
* be accessed through getEntity() and getEntities().
4245
*
43-
* @var Collection
46+
* @var array
4447
*/
4548
private $entities = array();
4649

@@ -50,7 +53,7 @@ class EntityChoiceList extends ArrayChoiceList
5053
*
5154
* This property should only be accessed through queryBuilder.
5255
*
53-
* @var Doctrine\ORM\QueryBuilder
56+
* @var EntityLoaderInterface
5457
*/
5558
private $entityLoader;
5659

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

66-
/**
67-
* A cache for the UnitOfWork instance of Doctrine
68-
*
69-
* @var Doctrine\ORM\UnitOfWork
70-
*/
71-
private $unitOfWork;
72-
7369
/**
7470
* Property path to access the key value of this choice-list.
7571
*
@@ -99,15 +95,15 @@ public function __construct(ObjectManager $manager, $class, $property = null, En
9995
$this->em = $manager;
10096
$this->class = $class;
10197
$this->entityLoader = $entityLoader;
102-
$this->unitOfWork = $manager->getUnitOfWork();
103-
$this->identifier = $manager->getClassMetadata($class)->getIdentifierFieldNames();
98+
$this->classMetadata = $manager->getClassMetadata($class);
99+
$this->identifier = $this->classMetadata->getIdentifierFieldNames();
104100
$this->groupBy = $groupBy;
105101

106102
// The property option defines, which property (path) is used for
107103
// displaying entities as strings
108104
if ($property) {
109105
$this->propertyPath = new PropertyPath($property);
110-
} else if (!method_exists($this->class, '__toString')) {
106+
} elseif (!method_exists($this->class, '__toString')) {
111107
// Otherwise expect a __toString() method in the entity
112108
throw new FormException('Entities passed to the choice field must have a "__toString()" method defined (or you can also override the "property" option).');
113109
}
@@ -305,10 +301,10 @@ public function getEntitiesByKeys(array $keys)
305301
*/
306302
public function getIdentifierValues($entity)
307303
{
308-
if (!$this->unitOfWork->isInIdentityMap($entity)) {
304+
if (!$this->em->contains($entity)) {
309305
throw new FormException('Entities passed to the choice field must be managed');
310306
}
311307

312-
return $this->unitOfWork->getEntityIdentifier($entity);
308+
return $this->classMetadata->getIdentifierValues($entity);
313309
}
314310
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Doctrine\Form\Type;
13+
14+
use Doctrine\Common\Persistence\ManagerRegistry;
15+
use Doctrine\Common\Persistence\ObjectManager;
16+
use Symfony\Component\Form\FormBuilder;
17+
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
18+
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
19+
use Symfony\Bridge\Doctrine\Form\EventListener\MergeCollectionListener;
20+
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntitiesToArrayTransformer;
21+
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntityToIdTransformer;
22+
use Symfony\Component\Form\AbstractType;
23+
24+
abstract class DoctrineType extends AbstractType
25+
{
26+
/**
27+
* @var ManagerRegistry
28+
*/
29+
protected $registry;
30+
31+
public function __construct(ManagerRegistry $registry)
32+
{
33+
$this->registry = $registry;
34+
}
35+
36+
public function buildForm(FormBuilder $builder, array $options)
37+
{
38+
if ($options['multiple']) {
39+
$builder
40+
->addEventSubscriber(new MergeCollectionListener())
41+
->prependClientTransformer(new EntitiesToArrayTransformer($options['choice_list']))
42+
;
43+
} else {
44+
$builder->prependClientTransformer(new EntityToIdTransformer($options['choice_list']));
45+
}
46+
}
47+
48+
public function getDefaultOptions(array $options)
49+
{
50+
$defaultOptions = array(
51+
'em' => null,
52+
'class' => null,
53+
'property' => null,
54+
'query_builder' => null,
55+
'loader' => null,
56+
'choices' => null,
57+
'group_by' => null,
58+
);
59+
60+
$options = array_replace($defaultOptions, $options);
61+
62+
if (!isset($options['choice_list'])) {
63+
$manager = $this->registry->getManager($options['em']);
64+
if (isset($options['query_builder'])) {
65+
$options['loader'] = $this->getLoader($manager, $options);
66+
}
67+
68+
$defaultOptions['choice_list'] = new EntityChoiceList(
69+
$manager,
70+
$options['class'],
71+
$options['property'],
72+
$options['loader'],
73+
$options['choices'],
74+
$options['group_by']
75+
);
76+
}
77+
78+
return $defaultOptions;
79+
}
80+
81+
/**
82+
* Return the default loader object.
83+
*
84+
* @param ObjectManager $manager
85+
* @param array $options
86+
* @return EntityLoaderInterface
87+
*/
88+
abstract protected function getLoader(ObjectManager $manager, array $options);
89+
90+
public function getParent(array $options)
91+
{
92+
return 'choice';
93+
}
94+
}

src/Symfony/Bridge/Doctrine/Form/Type/EntityType.php

Lines changed: 5 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,80 +11,22 @@
1111

1212
namespace Symfony\Bridge\Doctrine\Form\Type;
1313

14-
use Doctrine\Common\Persistence\ManagerRegistry;
14+
use Doctrine\Common\Persistence\ObjectManager;
1515
use Symfony\Component\Form\FormBuilder;
1616
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityChoiceList;
17+
use Symfony\Bridge\Doctrine\Form\ChoiceList\EntityLoaderInterface;
1718
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
18-
use Symfony\Bridge\Doctrine\Form\EventListener\MergeCollectionListener;
19-
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntitiesToArrayTransformer;
20-
use Symfony\Bridge\Doctrine\Form\DataTransformer\EntityToIdTransformer;
21-
use Symfony\Component\Form\AbstractType;
2219

23-
class EntityType extends AbstractType
20+
class EntityType extends DoctrineType
2421
{
25-
/**
26-
* @var ManagerRegistry
27-
*/
28-
protected $registry;
29-
30-
public function __construct(ManagerRegistry $registry)
31-
{
32-
$this->registry = $registry;
33-
}
34-
35-
public function buildForm(FormBuilder $builder, array $options)
36-
{
37-
if ($options['multiple']) {
38-
$builder
39-
->addEventSubscriber(new MergeCollectionListener())
40-
->prependClientTransformer(new EntitiesToArrayTransformer($options['choice_list']))
41-
;
42-
} else {
43-
$builder->prependClientTransformer(new EntityToIdTransformer($options['choice_list']));
44-
}
45-
}
46-
47-
public function getDefaultOptions(array $options)
48-
{
49-
$defaultOptions = array(
50-
'em' => null,
51-
'class' => null,
52-
'property' => null,
53-
'query_builder' => null,
54-
'loader' => null,
55-
'choices' => null,
56-
'group_by' => null,
57-
);
58-
59-
$options = array_replace($defaultOptions, $options);
60-
61-
if (!isset($options['choice_list'])) {
62-
$manager = $this->registry->getManager($options['em']);
63-
if (isset($options['query_builder'])) {
64-
$options['loader'] = $this->getLoader($manager, $options);
65-
}
66-
67-
$defaultOptions['choice_list'] = new EntityChoiceList(
68-
$manager,
69-
$options['class'],
70-
$options['property'],
71-
$options['loader'],
72-
$options['choices'],
73-
$options['group_by']
74-
);
75-
}
76-
77-
return $defaultOptions;
78-
}
79-
8022
/**
8123
* Return the default loader object.
8224
*
83-
* @param ObjectManager
25+
* @param ObjectManager $manager
8426
* @param array $options
8527
* @return ORMQueryBuilderLoader
8628
*/
87-
protected function getLoader($manager, $options)
29+
protected function getLoader(ObjectManager $manager, array $options)
8830
{
8931
return new ORMQueryBuilderLoader(
9032
$options['query_builder'],
@@ -93,11 +35,6 @@ protected function getLoader($manager, $options)
9335
);
9436
}
9537

96-
public function getParent(array $options)
97-
{
98-
return 'choice';
99-
}
100-
10138
public function getName()
10239
{
10340
return 'entity';

0 commit comments

Comments
 (0)
0