-
-
Notifications
You must be signed in to change notification settings - Fork 9.5k
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
[Form] Add flexibility for EntityType #13990
Conversation
@@ -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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
👍 |
Is it ok for merge? I added a unit test to cover this case. |
phpdoc for ORMQueryBuilderLoader::queryBuilder must be changed to |
What do you think about this @beberlei? Should we support |
ping @symfony/deciders @beberlei Any decisions on this one? |
👍 looks like a simple but effective solution |
Thank you @raziel057. |
The implementation of this feature was reverted by eb08baa, and was never meant to work because when the The only feature it adds is that The test is wrong as no entities are persisted, it should be: public function testConfigureQueryBuilderWithClosureReturningNull()
{
$entity1 = new SingleIntIdEntity(1, 'Foo');
$entity2 = new SingleIntIdEntity(2, 'Bar');
$this->persist(array($entity1, $entity2));
$field = $this->factory->createNamed('name', 'Symfony\Bridge\Doctrine\Form\Type\EntityType', null, array(
'em' => 'default',
'class' => self::SINGLE_IDENT_CLASS,
'query_builder' => function () {
return;
},
));
$this->assertSame(array(), $field->createView()->vars['choices']);
} Then it fails. The only way I see to support such use case is to create a custom type that handle its own configuration but the easiest way would be to set |
…s null (HeahDude) This PR was merged into the 2.8 branch. Discussion ---------- [Form] fixed EntityType test with query_builder option as null | Q | A | ------------- | --- | Branch? | 2.8+ | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | symfony/symfony-docs#6599 ref #13990 (comment). Commits ------- ad8e989 [Form] fixed EntityType test with query_builder option
…s null (HeahDude) This PR was merged into the 2.8 branch. Discussion ---------- [Form] fixed EntityType test with query_builder option as null | Q | A | ------------- | --- | Branch? | 2.8+ | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | ~ | License | MIT | Doc PR | symfony/symfony-docs#6599 ref symfony/symfony#13990 (comment). Commits ------- ad8e989 [Form] fixed EntityType test with query_builder option
This PR was merged into the 2.8 branch. Discussion ---------- Fixed null description of query_builder option doc fix: 2.8+ ref symfony/symfony#13990 (comment) Commits ------- 14482e4 Fixed null description of query_builder option
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:
To be able to present a list without entries when creating an entity field like this:
Rather than using the "choices" attributes (more verbose and which requires the injection of the entityManager):