Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 317d30b

Browse files
committed
feature #13990 [Form] Add flexibility for EntityType (raziel057)
This PR was submitted for the 2.7 branch but it was merged into the 2.8 branch instead (closes #13990). Discussion ---------- [Form] Add flexibility for EntityType | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | Sometimes it can be usefull to return null rather than a QueryBuilder from the closure attached to the ``query_builder`` attribute of an EntityType. For example, if I have the following method in a "DelegateRepository", which can return a QueryBuilder or null: ```php public function getQbForOfficers() { $permanentMeeting = $this->getEntityManager()->getRepository('MyBundle:PermanentMeeting')->findOneBy(array()); if ($permanentMeeting === null || $permanentMeeting->getMeeting() === null) { return null; } $qb = $this->getEntityManager()->createQueryBuilder(); $qb->select('p') ->from('MyBundle:Delegate', 'p') ->andWhere('p.meeting = :meetingId') ->setParameter('meetingId', $permanentMeeting->getMeeting()->getId()); return $qb; } ``` To be able to present a list without entries when creating an entity field like this: ```php $event->getForm()->add('officers', 'entity', array( 'class' => 'MyBundle:Delegate', 'property' => 'fullName', 'query_builder' => function(EntityRepository $er) use ($meeting, $delegationId) { return $er->getQbForOfficers(); } ))); ``` Rather than using the "choices" attributes (more verbose and which requires the injection of the entityManager): ```php $qb = $this->entityManager->getRepository('PTCNoventoBundle:Delegate')->getQbForOfficers(); $officers = ($qb !== null) ? $qb->getQuery()->getResult() : array(); $event->getForm()->add('officers', 'entity', array( 'class' => 'PTCNoventoBundle:Delegate', 'property' => 'fullName', 'choices' => $officers, )); ``` Commits ------- e0a1294 [Form] Add flexibility for EntityType
2 parents c856a01 + e0a1294 commit 317d30b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
3030
*
3131
* This property should only be accessed through queryBuilder.
3232
*
33-
* @var QueryBuilder
33+
* @var QueryBuilder|null
3434
*/
3535
private $queryBuilder;
3636

@@ -68,7 +68,7 @@ public function __construct($queryBuilder, $manager = null, $class = null)
6868

6969
$queryBuilder = $queryBuilder($manager->getRepository($class));
7070

71-
if (!$queryBuilder instanceof QueryBuilder) {
71+
if (null !== $queryBuilder && !$queryBuilder instanceof QueryBuilder) {
7272
throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder');
7373
}
7474
}
@@ -81,6 +81,10 @@ public function __construct($queryBuilder, $manager = null, $class = null)
8181
*/
8282
public function getEntities()
8383
{
84+
if (null === $this->queryBuilder) {
85+
return array();
86+
}
87+
8488
return $this->queryBuilder->getQuery()->execute();
8589
}
8690

@@ -89,6 +93,10 @@ public function getEntities()
8993
*/
9094
public function getEntitiesByIds($identifier, array $values)
9195
{
96+
if (null === $this->queryBuilder) {
97+
return array();
98+
}
99+
92100
$qb = clone ($this->queryBuilder);
93101
$alias = current($qb->getRootAliases());
94102
$parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;

src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,19 @@ public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()
212212

213213
$field->submit('2');
214214
}
215+
216+
public function testConfigureQueryBuilderWithClosureReturningNull()
217+
{
218+
$field = $this->factory->createNamed('name', 'entity', null, array(
219+
'em' => 'default',
220+
'class' => self::SINGLE_IDENT_CLASS,
221+
'query_builder' => function () {
222+
return null;
223+
},
224+
));
225+
226+
$this->assertEquals(array(), $field->createView()->vars['choices']);
227+
}
215228

216229
public function testSetDataSingleNull()
217230
{

0 commit comments

Comments
 (0)