Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>First of all, you have to create "dynamically" your choices based onto user selection.<br/> There are two ways, strictly coupled with your application behaviour, that leads (in both cases) at the same "core" code; the difference is only the way you call it.</p> <p>So, let's explain the two possibilities:</p> <ol> <li>Select manufacturer "statically" and don't change it over the time of request: use data retrieved from your business logic</li> <li>Select manufacturer "dinamically" and can change them over the time of request: replace your form at every request; new form will be provided by business logic</li> </ol> <p>So, the <em>HEART</em> of your question is: how can I retrieve only associated entities?</p> <h3>Answer</h3> <p>Into your VehicleType.php (where entity, in this case, is VehicleType ?)</p> <pre><code>private $manufacturerId; public function __construct($manufacturerId) { $this-&gt;manufacturerId = $manufacturerId; } public function BuildForm(FormBuilderInterface $builder) { $manufacturerId = $this-&gt;manufacturerId; $manufacturerQuery = function(EntityRepository $repo) use ($manufacturerId ){ return $repo-&gt;createQueryBuilder('m') -&gt;where('m.id = :id') -&gt;setParameter('id',$manufacturerId );}; $builder-&gt;add('manufacturer', 'entity', array( 'label' =&gt; 'Brand', 'attr' =&gt; array('class' =&gt; 'input-block-level'), 'class' =&gt; 'BalslevCarBundle:Manufacturers', 'property' =&gt; 'short_name', 'empty_value' =&gt; 'All', 'required' =&gt; false, 'query_builder' =&gt; $manufacturerQuery; )); $builder-&gt;add('model', 'entity', array( 'label' =&gt; 'Brand', 'attr' =&gt; array('class' =&gt; 'input-block-level'), 'class' =&gt; 'BalslevCarBundle:Manufacturers', 'property' =&gt; 'short_name', 'empty_value' =&gt; 'All', 'required' =&gt; false, 'query_builder' =&gt; $modelQuery; )); } </code></pre> <p>As you can see, in this way we use a custom repository to get only entities that are suitable for our needs. Obviously I am writing my DQL query without knowing your DB structure: it's your task and responsibility to adapt it to your real situation.</p> <p>Moreover, you have to create another query for model (called <code>$modelQuery</code> in my answer) to retrieve correct models based on selected <code>$id</code></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload