1

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

4

2 回答 2

1

您必须通过编辑实体的方法手动Audit在实体中设置实体。RessourceaddRessource()

只需编辑此方法,如:

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

    return $this;
}

UPD

然后尝试在控制器中手动持久化资源:

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

UPD

嗯,奇怪。执行下一个代码片段并检查资源关系:

$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();
于 2013-03-21T07:41:43.130 回答
0

好吧,我想我知道发生了什么。这个更新的控制器应该可以解决您的问题:

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()));
}
于 2013-03-20T18:08:42.033 回答