doctrine-extensions / DoctrineExtensions

Doctrine2 behavioral extensions, Translatable, Sluggable, Tree-NestedSet, Timestampable, Loggable, Sortable
MIT License
4.05k stars 1.27k forks source link

[Translatable] Set hint and return queryBuilder for using with symfony2 entity form type. #835

Closed brpaz closed 2 years ago

brpaz commented 11 years ago

Hi. In Symfony2 you can define entity form type which will fetch the records from a entity table from the database. (see: http://symfony.com/doc/current/reference/forms/types/entity.html#using-a-custom-query-for-the-entities) That entity represents a country so I have more than 200 records. In result of that Doctrine is executing one query for each row which results in more than 200 queries just to populate a select box!

The implemention of Entity Form type allows me to pass a query builder object to set my query. The problem is that I can only setQueryHint in Query objects. Is there any way I can "convert" the query into QueryBuilder again or something like that?. Thanks you.

l3pp4rd commented 11 years ago

No you can't, would be actually good if query builder could proxy query hints, but its not yet possible in doctrine2 ORM. So what you can do is to create a custom entity loader for your form choice list. I have made it in extension demo: https://github.com/l3pp4rd/gediminasm.org/blob/symfony2/src/Gedmo/DemoBundle/Form/ChoiceList/CategoryEntityLoader.php#L31 Its not a convenient way, but that was what I came up with.. Maybe someone could give a better advice

brpaz commented 11 years ago

hmm, I didnt know about Entity Loaders. Will check that later. Thanks.

jdespatis commented 10 years ago

Hello I've exactly the same need as @brpaz I'm checking the solution exposed by @l3pp4rd Is there some better solution now ? (as last comment was from a year ago, things may have changed...)

rredpoppy commented 9 years ago

anyone?

Cedoriku commented 9 years ago

Hi everyone, @l3pp4rd I was on my way to create my own EntityLoader as well, but I was wondering how to use it after that? How do you tell your formType to use this one instead of Doctrine's default?

Thank you

l3pp4rd commented 9 years ago

Hi @Cedoriku the symfony version I have used was like 2.1 then. Now it is easier to create a type with an entity manager or repository reference and build choice list in constructor of type wanted. I do not think there are better ways, I do it simply like that myself when I need to. Passing repository or choices through type constructor..

Cedoriku commented 9 years ago

@l3pp4rd Thank you for taking time to answer, and by the way thank you for the Translatable bundle.

Is there any advantage in using the 'choice_list' option rather than setting a 'choices' array by ourselves using simple repository requests?

Creating, instanciating and passing a custom EntityChoiceList for all entity choice form types in the application seems rather complicated to me, compared to setting a result set directly to the 'choices' option.. Not to speak about specific customized queries that we could need.. I don't really understand the need of ChoiceList here.

l3pp4rd commented 9 years ago

Thanks. There is no advantage to use choice_list, use whatever works for you better. In the time I was developing it, there were less options in symfony form component, that was the only reason...

lukepass commented 4 years ago

I posted a similar issue here: https://github.com/symfony/symfony/issues/37582

github-actions[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.