Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Concerning what you already tried, I think you should retry your first/second ideas:</p> <blockquote> <p>My first idea was to use the form entity type, thinking the component could be binded somehow on another field. ie. update list of employees based on the value of the selected company.</p> </blockquote> <p>You can populate the employees select box using the <code>entity</code> type. All you have to do is to define the good options:</p> <pre class="lang-php prettyprint-override"><code>class FooType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder -&gt;add('employee', 'entity', array( 'class' =&gt; 'Entity\Employee', 'query_builder' =&gt; function ($repository) use($options) { return $repository -&gt;createQueryBuilder('e') -&gt;where('e.company = :company') -&gt;setParameter('company', $options['companyId']) ; }, )) ; } public function getDefaultOptions(array $options) { return array('data_class' =&gt; 'Entity\Foo', 'companyId' =&gt; null); } } </code></pre> <blockquote> <p>Then I thought about manually passing the selected company as parameter to the query builder of the second dropdown list.</p> </blockquote> <p>The example here filters the employees list based on the companyId form's option. You can modify this behavior by filtering directly on the company present in the form's data.</p> <pre class="lang-php prettyprint-override"><code>public function buildForm(FormBuilder $builder, array $options) { $companyId = $builder-&gt;getData()-&gt;getCompanyId(); $builder -&gt;add('employee', 'entity', array( 'class' =&gt; 'Entity\Employee', 'query_builder' =&gt; function ($repository) use ($companyId) { return $repository -&gt;createQueryBuilder('e') -&gt;where('e.company = :company') -&gt;setParameter('company', $companyId) ; }, )) ; } </code></pre> <p>You still have to implement the <code>getEmployee()</code> and <code>setEmployee()</code> methods in your <code>Entity\Foo</code> class.</p> <blockquote> <p>But when the form is created, the values are empty. The values are only set on bindRequest.</p> </blockquote> <p>No. Values are set when you create you form using form factory (third argument), OR when you call <code>$form-&gt;setData($foo);</code>. Data is modified when you <code>bind</code> new inputs to the form.</p> <p><em>There could be a problem with this approach: it's possible that the employee id bound to the form is not available in the form choice list, because you changed the company (and thus the employees).</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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