Skip to content
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

Closed
wants to merge 4 commits into from
Closed

Conversation

raziel057
Copy link
Contributor

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:

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:

$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):

$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,
));

@@ -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.

@fabpot
Copy link
Member

fabpot commented Mar 24, 2015

👍

@raziel057
Copy link
Contributor Author

Is it ok for merge? I added a unit test to cover this case.

@Tobion
Copy link
Contributor

Tobion commented Mar 24, 2015

phpdoc for ORMQueryBuilderLoader::queryBuilder must be changed to @var QueryBuilder|null

@webmozart webmozart added the Form label Jun 16, 2015
@webmozart
Copy link
Contributor

What do you think about this @beberlei? Should we support null or should we return a query builder that returns no entities instead?

@fabpot
Copy link
Member

fabpot commented Aug 1, 2015

ping @symfony/deciders @beberlei Any decisions on this one?

@xabbuh
Copy link
Member

xabbuh commented Aug 1, 2015

👍 looks like a simple but effective solution

@fabpot
Copy link
Member

fabpot commented Aug 1, 2015

Thank you @raziel057.

@fabpot fabpot closed this in 317d30b Aug 1, 2015
@fabpot fabpot mentioned this pull request Nov 16, 2015
@HeahDude
Copy link
Contributor

The implementation of this feature was reverted by eb08baa, and was never meant to work because when the query_builder option is set to null (which is the default), whether or not returned by a callable, all entities are loaded.

The only feature it adds is that null can be set conditionally via the callable.

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 choices option to an empty array.

fabpot added a commit that referenced this pull request Jun 15, 2016
…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
symfony-splitter pushed a commit to symfony/doctrine-bridge that referenced this pull request Jun 15, 2016
…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
wouterj added a commit to symfony/symfony-docs that referenced this pull request Jun 20, 2016
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants