8000 [Form] EntityType in forms generates too many queries · Issue #50898 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] EntityType in forms generates too many queries #50898

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
passwebcakebat opened this issue Jul 6, 2023 · 8 comments
Closed

[Form] EntityType in forms generates too many queries #50898

passwebcakebat opened this issue Jul 6, 2023 · 8 comments

Comments

@passwebcakebat
Copy link

Symfony version(s) affected

5.4.24

Description

The EntityType field in forms generates too many queries in the POST action.

If field is multiple and user select about 15/20 options POST method add additional queries in each options.

I know that the EntityType want to check if selected option is in our database.
But why does it work in foreach and send query through each selected options.

In ORMQueryBuilderLoader.php there is a method called getEntitiesByIds.
But when we see how it works we can see that it works in foreach of our selected options.

When we add additional inserts and selections the database queries are too large.

How to reproduce

$builder->add('test', EntityType::class, [ 'label' => 'Label test', 'class' => YourClass::class, 'multiple' => true, 'required' => false, 'choice_label' => 'name', 'choice_value' => 'id', ]);

Possible Solution

Maybe there is the chance to change this foreach to one request with all selected options with IN array.

Additional Context

No response

@passwebcakebat passwebcakebat changed the title EntityType in forms generates too many queries [Form] EntityType in forms generates too many queries Jul 6, 2023
@stof
Copy link
Member
stof commented Jul 6, 2023

We will need a reproducer for that.

I strongly doubt that your suggestion of the cause is right. getEntitiesByIds loads all the entities for the list of values as a single query using IN

@passwebcakebat
Copy link
Author

So the problem is in another place. Because when I debuging that function, variable method get only one parameter not whole array.

image
etc.

@stof
Copy link
Member
stof commented Jul 6, 2023

again, please provide a reproducer.

@passwebcakebat
Copy link
Author

Hi,
https://github.com/passwebcakebat/Symfony-5.4-EntityTypeBug
I'm sending repo with reproducer.

Symfony ver 5.4
database SQLLite

@xabbuh
Copy link
Member
xabbuh commented Jul 11, 2023

What does one have to in that application to reproduce what you experience?

@passwebcakebat
Copy link
Author

Put debug mark in getEntitiesByIds method, next just add task in /tasks/new

getEntitiesByIds method get value by each and in last call this method get all values

@xabbuh
Copy link
Member
xabbuh commented Jul 11, 2023

I think I understand the issue now. It seems this is a regression caused by #39659. Can you please try if #50937 fixes this for you?

@passwebcakebat
Copy link
Author

#50937 fixed this isssue, thanks

fabpot added a commit that referenced this issue Jul 13, 2023
This PR was merged into the 5.4 branch.

Discussion
----------

[Form] fetch all known ChoiceType values at once

| Q             | A
| ------------- | ---
| Branch?       | 5.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | Fix #50898
| License       | MIT
| Doc PR        |

Commits
-------

5e0f832 fetch all known ChoiceType values at once
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
0