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

时间:2013-03-20 17:51:23

标签: php symfony orm doctrine-orm symfony-forms

我有一个审核表单,我可以附加几个资源 (审计有很多资源,资源只附加到一个审计 )。

表单在视图中很好地呈现,但是,在保存表单时,所选的资源不会附加到数据库中的审计。

审核表格:

>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:在Ressource表单上,当我选择审核时,它已正确保存

控制器:

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)显示:

private 'slug' => null
  private 'ressources' => 
    object(Doctrine\Common\Collections\ArrayCollection)[4190]
      private '_elements' => 
        array
          0 => 
            object(Spri\MonitorBusinessBundle\Entity\Ressource)[3766]
              ...
  privat...

任何想法??

2 个答案:

答案 0 :(得分:1)

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

只需编辑此方法,如:

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

    return $this;
}

<强> UPD

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

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

<强> UPD

嗯,奇怪。执行下一个代码段并检查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();

答案 1 :(得分: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()));
}