Skip to content

[Form] Add flexibility for EntityType #13990

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class ORMQueryBuilderLoader implements EntityLoaderInterface
*
* This property should only be accessed through queryBuilder.
*
* @var QueryBuilder
* @var QueryBuilder|null
*/
private $queryBuilder;

Expand Down Expand Up @@ -55,7 +55,7 @@ public function __construct($queryBuilder, $manager = null, $class = null)

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

if (!$queryBuilder instanceof QueryBuilder) {
if (null !== $queryBuilder && !$queryBuilder instanceof QueryBuilder) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is not needed AFAIR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove this condition, I will get the UnexpectedTypeException if the Closure returns null. So we need to let null as supported value here.

throw new UnexpectedTypeException($queryBuilder, 'Doctrine\ORM\QueryBuilder');
}
}
Expand All @@ -68,6 +68,10 @@ public function __construct($queryBuilder, $manager = null, $class = null)
*/
public function getEntities()
{
if (null === $this->queryBuilder) {
return array();
}

return $this->queryBuilder->getQuery()->execute();
}

Expand All @@ -76,6 +80,10 @@ public function getEntities()
*/
public function getEntitiesByIds($identifier, array $values)
{
if (null === $this->queryBuilder) {
return array();
}

$qb = clone ($this->queryBuilder);
$alias = current($qb->getRootAliases());
$parameter = 'ORMQueryBuilderLoader_getEntitiesByIds_'.$identifier;
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Bridge/Doctrine/Tests/Form/Type/EntityTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,19 @@ public function testConfigureQueryBuilderWithClosureReturningNonQueryBuilder()

$field->submit('2');
}

public function testConfigureQueryBuilderWithClosureReturningNull()
{
$field = $this->factory->createNamed('name', 'entity', null, array(
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'query_builder' => function () {
return null;
},
));

$this->assertEquals(array(), $field->createView()->vars['choices']);
}

public function testSetDataSingleNull()
{
Expand Down