-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #52503 [DoctrineBridge][Form] Introducing new `LazyChoiceLoad…
…er` class and `choice_lazy` option for `ChoiceType` (yceruto) This PR was merged into the 7.2 branch. Discussion ---------- [DoctrineBridge][Form] Introducing new `LazyChoiceLoader` class and `choice_lazy` option for `ChoiceType` | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | no | New feature? | yes | Deprecations? | no | Issues | #57724 | License | MIT It's quite usual to work with forms that process large datasets. In Symfony Form + Doctrine ORM, if you define an `EntityType`, it typically loads all choices/entities fully into memory, and this can lead to serious performance problems if your entity table contain several hundred or thousands of records. The new `LazyChoiceLoader` class addresses this performance issue by implementing an on-demand choice loading strategy. This class is integrated with any `ChoiceType` subtype by using a new boolean option named `choice_lazy`, which activates the feature. Basic usage in a Symfony form looks like this: ```php $formBuilder->add('user', EntityType::class, [ 'class' => User::class, // a ton of users... 'choice_lazy' => true, ]); ``` **How does it work?** The loader operates by keeping the choice list empty until values are needed (avoiding unnecessary database queries). When form values are provided or submitted, it retrieves and caches only the necessary choices. As you can see in the code, all this happens behind the `LazyChoiceLoader` class, which delegates the loading of choices to a wrapped `ChoiceLoaderInterface` adapter (in this case, the `DoctrineChoiceLoader`). **Frontend Considerations** Certainly, you may need a JavaScript component for dynamically loading `<select>` options, aka autocomplete plugins. You'll need to develop the endpoint/controller to fetch this data on your own, ensuring it corresponds to the form field data source. This aspect is not included in this project. As a point of reference, the [Autocomplete UX Component](https://symfony.com/bundles/ux-autocomplete/current/index.html) now uses this choice loading strategy, simplifying its autocomplete form type to a single field: <img src="https://symfony.com/doc/bundles/ux-autocomplete/2.x/ux-autocomplete-animation.gif"/> **A Handy Use Case without Javascript?** The `disabled` option renders an `EntityType` form field read-only, and when combined with the `choice_lazy` option, it prevents the loading of unnecessary entities in your choice list (only the pre-selected entities will be loaded), thereby enhancing performance. --- Hope this helps to create simpler autocomplete components for Symfony forms. Cheers! Commits ------- d73b5ee add LazyChoiceLoader and choice_lazy option
- Loading branch information
Showing
8 changed files
with
374 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/Symfony/Component/Form/ChoiceList/Loader/LazyChoiceLoader.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\ChoiceList\Loader; | ||
|
||
use Symfony\Component\Form\ChoiceList\ArrayChoiceList; | ||
use Symfony\Component\Form\ChoiceList\ChoiceListInterface; | ||
|
||
/** | ||
* A choice loader that loads its choices and values lazily, only when necessary. | ||
* | ||
* @author Yonel Ceruto <yonelceruto@gmail.com> | ||
*/ | ||
class LazyChoiceLoader implements ChoiceLoaderInterface | ||
{ | ||
private ?ChoiceListInterface $choiceList = null; | ||
|
||
public function __construct( | ||
private readonly ChoiceLoaderInterface $loader, | ||
) { | ||
} | ||
|
||
public function loadChoiceList(?callable $value = null): ChoiceListInterface | ||
{ | ||
return $this->choiceList ??= new ArrayChoiceList([], $value); | ||
} | ||
|
||
public function loadChoicesForValues(array $values, ?callable $value = null): array | ||
{ | ||
$choices = $this->loader->loadChoicesForValues($values, $value); | ||
$this->choiceList = new ArrayChoiceList($choices, $value); | ||
|
||
return $choices; | ||
} | ||
|
||
public function loadValuesForChoices(array $choices, ?callable $value = null): array | ||
{ | ||
$values = $this->loader->loadValuesForChoices($choices, $value); | ||
|
||
if ($this->choiceList?->getValuesForChoices($choices) !== $values) { | ||
$this->loadChoicesForValues($values, $value); | ||
} | ||
|
||
return $values; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
src/Symfony/Component/Form/Tests/ChoiceList/Loader/LazyChoiceLoaderTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Tests\ChoiceList\Loader; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Form\ChoiceList\Loader\LazyChoiceLoader; | ||
use Symfony\Component\Form\Tests\Fixtures\ArrayChoiceLoader; | ||
|
||
class LazyChoiceLoaderTest extends TestCase | ||
{ | ||
private LazyChoiceLoader $loader; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->loader = new LazyChoiceLoader(new ArrayChoiceLoader(['A', 'B', 'C'])); | ||
} | ||
|
||
public function testInitialEmptyChoiceListLoading() | ||
{ | ||
$this->assertSame([], $this->loader->loadChoiceList()->getChoices()); | ||
} | ||
|
||
public function testOnDemandChoiceListAfterLoadingValuesForChoices() | ||
{ | ||
$this->loader->loadValuesForChoices(['A']); | ||
$this->assertSame(['A' => 'A'], $this->loader->loadChoiceList()->getChoices()); | ||
} | ||
|
||
public function testOnDemandChoiceListAfterLoadingChoicesForValues() | ||
{ | ||
$this->loader->loadChoicesForValues(['B']); | ||
$this->assertSame(['B' => 'B'], $this->loader->loadChoiceList()->getChoices()); | ||
} | ||
|
||
public function testOnDemandChoiceList() | ||
{ | ||
$this->loader->loadValuesForChoices(['A']); | ||
$this->loader->loadChoicesForValues(['B']); | ||
$this->assertSame(['B' => 'B'], $this->loader->loadChoiceList()->getChoices()); | ||
} | ||
} |
Oops, something went wrong.