Symfony 2形式:具有多对一/一对多关系的实体小部件 - 关系不持久

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??

Alright, I think I figured out what's going on. This updated controller should fix your issue:

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);

            $newItem = $form->getData();

            $em->persist($newItem);
            $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()));
}

You have to set Audit entity in the Ressource entities manually by editing the addRessource() method of the entity.

Just edit this method like:

public function addRessource(Ressource $ressource)
{
    $this->ressources[] = $ressource;
    $ressource->setAudit($this);

    return $this;
}

UPD

Then try to persist ressources manually in the controller:

foreach ($audit->getRessources() as $ressource) {
    $em->persist($ressource);
}

UPD

Well, strange. Execute the next code snippet and check ressources relation:

$em = $this->getDoctrine()->getManager();

$ressource = new Ressource();
$ressource->setSomeRequiredFieldValue($someValue);

$audit = new Audit();
$audit->setSomeRequiredFieldValue($someValue);
$audit->addRessource($ressource);

$em->persist($audit);
$em->persist($ressource);
$em->flush();