Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony 2 form : entity widget with many to one/one to many relation - relation not persisting
    text
    copied!<p>I have a Audit form, to which i can attach several ressources <strong><em>(Audit has many Ressources, a ressource is attached to only one audit</em></strong>).</p> <p>the form is well presented in the view, yet, when saving the form, the selected ressources are not attached to the audit in the DB.</p> <p>Audit Form :</p> <pre><code>&gt;add('ressources', 'entity', array( 'class' =&gt; 'SpriMonitorBusinessBundle:Ressource', 'query_builder' =&gt; $this-&gt;em-&gt;getRepository('SpriMonitorBusinessBundle:Ressource')-&gt;getAvailableRessources(true), 'multiple'=&gt;true )) </code></pre> <p>Audit.orm.yml:</p> <pre><code>oneToMany: ressources: targetEntity: Ressource mappedBy: audit </code></pre> <p>Ressource.orm.yml:</p> <pre><code>manyToOne: audit: targetEntity: Audit inversedBy: ressources joinColumn: name: audit_id referencedColumnName: id </code></pre> <p><em><strong>N.B: On the Ressource form, when i select an audit, it is correctly saved</em></strong></p> <p>Controller:</p> <pre><code>public function newAction() { $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); $item = AuditFactory::make(); $form = $this-&gt;createForm(new AuditType($em),$item); $request = $this-&gt;get('request'); $session = $this-&gt;get('session'); if ('POST' == $request-&gt;getMethod()) { try { $this-&gt;validateForm($form,$request); $em-&gt;persist($item); $em-&gt;flush(); $message = $this-&gt;container-&gt;getParameter('form_submit_success'); $session-&gt;setFlash('success', $message); $url = $this-&gt;generateUrl('Spri_audit_list'); return $this-&gt;redirect($url); } catch (FormException $e) { $session-&gt;setFlash('error', sprintf('Erreur Formulaire : "%s"',$e-&gt;getMessage())); } catch (\Exception $e) { die(var_dump($e-&gt;getMessage())); $session-&gt;setFlash('error', sprintf('Erreur inconnue ! Contactez l\'ADMIN'.$e-&gt;getMessage())); } } return $this-&gt;render('SpriAuditBundle:Audit:new.html.twig', array('form'=&gt;$form-&gt;createView())); } protected function validateForm($form, $request) { $form-&gt;bind($request); if (!$form-&gt;isValid()) { $message = $this-&gt;container-&gt;getParameter('form_submit_error'); throw new FormException($message); } } </code></pre> <p>var_dump($item) shows:</p> <pre><code>private 'slug' =&gt; null private 'ressources' =&gt; object(Doctrine\Common\Collections\ArrayCollection)[4190] private '_elements' =&gt; array 0 =&gt; object(Spri\MonitorBusinessBundle\Entity\Ressource)[3766] ... privat... </code></pre> <p>Any idea??</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