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 に答える