8000 [Form] Added an AbstractChoiceLoader by HeahDude · Pull Request #30983 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Added an AbstractChoiceLoader #30983

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

Conversation

HeahDude
Copy link
Contributor
@HeahDude HeahDude commented Apr 7, 2019
Q A
Branch? master
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets ~
License MIT
Doc PR TODO

Follow up #30962 and #30966.

Based on #30966 , only review lat commit until it is merged.

#EUFOSSA

@HeahDude HeahDude force-pushed the form/abstract-choice-loader branch from 6f3332f to 82e1931 Compare April 7, 2019 13:51
Copy link
Contributor
@vudaltsov vudaltsov left a comment

Choose a reason for hiding this comment

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

Looks great!


if ($value) {
// if a value callback exists, use it
return array_map($value, $choices);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is actually a new optimisation that will make some good when using choice_value option :)

@nicolas-grekas nicolas-grekas added this to the next milestone Apr 7, 2019
public function loadValuesForChoices(array $choices, $value = null)
{
// Optimize
if (empty($choices)) {
Copy link
Member

Choose a reason for hiding this comment

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

Some implementations use $values = array_filter($values); before the test. Is it something we can/want to add here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It happens that an empty value may be a valid choice for any choice list, but we know it's not the case with intl base choice types, this is why we optimized their loaders specifically.

public function loadChoicesForValues(array $values, $value = null)
{
// Optimize
if (empty($values)) {
Copy link
Member

Choose a reason for hiding this comment

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

if (!$values)?

@fabpot
Copy link
Member
fabpot commented Jul 8, 2019

@HeahDude What's the status of this PR? Are you willing to finish it?

@yceruto
Copy link
Member
yceruto commented Aug 16, 2019

Taking over in #33218

@fabpot fabpot closed this Aug 17, 2019
@nicolas-grekas nicolas-grekas modified the milestones: next, 4.4 Oct 27, 2019
fabpot added a commit that referenced this pull request Feb 12, 2020
…mentations and handle global optimizations (HeahDude)

This PR was merged into the 5.1-dev branch.

Discussion
----------

[Form] Added an AbstractChoiceLoader to simplify implementations and handle global optimizations

| Q             | A
| ------------- | ---
| Branch?       | master
| Bug fix?      | no
| New feature?  | yes
| Deprecations? | no
| Tickets       | ~
| License       | MIT
| Doc PR        | ~
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->
Taking over #33218 (taking over #30983)

The `ChoiceLoaderInterface` is not easy to understand/implement, while its goal is simple, lazy load an array of choices.
What may seem complicated is the how/what to optimize loading, we need to deal with a `$value` callback (which refers to the `choice_value` option allowing to transform a model choice to a unique string value that can be displayed/submitted).

We have now enough implementations in core to justify the need of an abstraction and provide a better DX in the process.

Before this PR we needed to implement 3 methods to create a loader:
 - `loadChoiceList(?callable $value): ChoiceListInterface`
 - `loadChoicesForValues(array $values, ?callable $value): array`
 - `loadValuesForChoices(array $choices, ?callable $value): array`
and handle optimization, in each.

Now we only need to implement:
 - `loadChoices():  iterable`

and optionnally:
 - `doLoadChoicesForValues(array $values, ?callable $value): array`
if more optimization is needed to only load submitted values, (i.e the core intl loader prevents loading values that are the same as choices, and the doctrine one performs a `WHERE id IN ($ids)` query).

Commits
-------

1394df2 [Form] Added an AbstractChoiceLoader to simplify implementations and handle global optimizations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0