Question

So, I'm looking for a solution for the following problem that would be "best practice" to implement in Symfony2.3.

Short description of situation: I've got a form defined in it's own class ("ProductType") The form contains a field that is linked to a different table. User needs to select 1 value from that list.

I want to restrict the values the user can choose from. Not all options that are found in the database are relevant. I need to dynamically reduce the options in the list based on information in the database and the current values already applied to the form. I can do this by adding a "Subscription" in which I change the contents of the form. But in this subscription I need access to the database/Doctrine Repository's to determine the list that the user may choose from.

So I need to inject Doctrine in the Subscription, but it is not available in the Type-class.

What is the best solution for this? - define the Subscription class as a service in Symfony and inject Doctrine via that route? - something else?

The Symfony documentation isn't very clear on this unfortunately.

Was it helpful?

Solution

You need to use the query builder, so you need the repository. And to do so just use

use Doctrine\ORM\EntityRepository; 

in your ProductType, as it's explained in the doc

OTHER TIPS

i'm not sure if i got what you really want to do, but if you want to reduce a number of elements (insisde e.g dropdown) depends on value, use query builder for entity field type:

http://symfony.com/doc/current/reference/forms/types/entity.html#query-builder

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top