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