Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect multiple into a Symfony2 FORM
    text
    copied!<p>I have this scenario</p> <p><code>EntityA</code> related to <code>EntityB</code> into a <strong>one-to-many</strong> fashion. I have created a form for <code>EntityB</code> and I want to select (with checkboxes) some of <code>EntityA</code> element.<br/></p> <p>How can I achieve this?</p> <p>UPDATE</p> <pre><code>public function viewAction() { $id_struttura = 9; $channel_rates = $this-&gt;getDoctrine()-&gt;getRepository('SestanteChannelManagerBundle:StrutturaCanaleTariffa')-&gt;findByStruttura($id_struttura); $list = array(); foreach ($channel_rates as $channel_rate) { $list[$channel_rate-&gt;getId()] = $channel_rate-&gt;getNomeCameraTariffa(); } $view = new View(); $view-&gt;addChannelRate($channel_rates); $form = $this-&gt;createForm(new ViewType(), $view); $request = $this-&gt;getRequest(); if ($request-&gt;getMethod() == 'POST') { $form-&gt;bindRequest($request); if ($form-&gt;isValid()) { $dateRange = new DateRange($date_from, $date_to); $channelDisp = $this-&gt;get('channel_dispatcher'); $inventories = $channelDisp-&gt;queryAvailabilityRates($dateRange, $channelRateList); return $this-&gt;render('SestanteCDTestBundle:Main:view_result.html.twig', array('inventories' =&gt; $inventories)); } } return $this-&gt;render('SestanteCDTestBundle:Main:view.html.twig', array('form' =&gt; $form-&gt;createView())); } </code></pre> <p>This is my entity</p> <pre><code>&lt;?php namespace Sestante\CDTestBundle\Entity; use Doctrine\Common\Collections\ArrayCollection; class View { protected $channel_rate; function __construct() { $this-&gt;channel_rate = new ArrayCollection(); } /** * @return the $date_from */ public function getDateFrom() { return $this-&gt;date_from; } /** * @return the $date_to */ public function getDateTo() { return $this-&gt;date_to; } /** * @return the $channel_rate */ public function getChannelRate() { return $this-&gt;channel_rate; } /** * @param field_type $date_from */ public function setDateFrom($date_from) { $this-&gt;date_from = $date_from; } /** * @param field_type $date_to */ public function setDateTo($date_to) { $this-&gt;date_to = $date_to; } /** * @param field_type $channel_rate */ public function addChannelRate($channel_rate) { $this-&gt;channel_rate[] = $channel_rate; } } </code></pre> <p>And this is my form</p> <pre><code>&lt;?php namespace Sestante\CDTestBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; class ViewType extends AbstractType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder-&gt;add('date_from', 'date', array('widget' =&gt; 'single_text','format' =&gt; 'yyyy-MM-dd')); $builder-&gt;add('date_to', 'date', array('widget' =&gt; 'single_text','format' =&gt; 'yyyy-MM-dd')); $builder-&gt;add('channel_rate', 'entity', array('class'=&gt;'SestanteChannelManagerBundle:StrutturaCanaleTariffa', 'multiple'=&gt;true, 'expanded'=&gt;true)); } public function getName() { return 'view'; } } </code></pre> <p>If I add a statement like this (into entity) all works good:</p> <pre><code>foreach ($channel_rate as $ch) { $this-&gt;channel_rate[] = $ch; } </code></pre>
 

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