简体   繁体   English

Symfony2 OneToMany嵌入表单不保存实体

[英]Symfony2 OneToMany embeded form doesn't save entity on

I have persistent problems with OneToMany embedded forms in Symfony2. 我在Symfony2中遇到OneToMany嵌入式表单的问题。

This time, the entities are saved, but not the reference to the parent class. 这次,实体被保存,但不是对父类的引用。

My table looks like that 我的桌子看起来像那样

| id | position | content | about_id |
| 29 |        1 |  test 1 |     NULL |

I cannot understand why this about_id is still NULL. 我无法理解为什么这个about_id仍然是NULL。

My relations: 我的关系:

Entity About: 实体关于:

class About {
    /*
     ....
    */

    /**
     * @ORM\OneToMany(targetEntity="AboutContent", mappedBy="about", cascade={"persist", "remove"})
     */
    private $content;

    /**
     * Add content
     *
     * @param AboutContent $content
     * @return About
     */
    public function addContent(AboutContent $content)
    {
        $this->content[] = $content;

        return $this;
    }

    /**
     * Remove content
     *
     * @param AboutContent $content
     */
    public function removeContent(AboutContent $content)
    {
        $this->content->removeElement($content);
    }

    /**
     * Get content
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getContent()
    {
        return $this->content;
    }
}

My About Content: 我的内容:

class AboutContent {
    /**
     * @ORM\ManyToOne(targetEntity="About", inversedBy="content")
     */
    private $about;

    /**
     * Set about
     *
     * @param About $about
     * @return AboutContent
     */
    public function setAbout(About $about = null)
    {
        $this->about = $about;

        return $this;
    }

    /**
     * Get about
     *
     * @return About 
     */
    public function getAbout()
    {
        return $this->about;
    }
}

My controller has been auto generated by my crud: 我的控制器已由我的crud自动生成:

/**
 * Creates a new About entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new About();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('about_show', array('id' => $entity->getId())));
    }

    return $this->render('AdminBundle:About:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

My form builder: 我的表单构建器:

$builder
  ->add('lang', 'choice', array(
                    'choices'   => $this->langs,
                    'preferred_choices' => array('en'),
        ))            ->add('media')
    ->add('content', 'collection', array(
        'type'         => new AboutContentType(),
        'allow_add'    => true,
        'allow_delete' => true,
        'cascade_validation' => true
      ), array('label'=>'Texts'))       
 ;

 }

Thank you very much if you find the solution. 非常感谢您找到解决方案。

Hi all after hous of search, I finally find the answer. 大家好,经过搜索,我终于找到了答案。 The problem did not came from my entities or my relation ships. 问题不是来自我的实体或我的关系船。 It came from my form. 它来自我的表格。

In my type, I just omitted to add the tag: 'by_reference' => false 在我的类型中,我只是省略了添加标记: 'by_reference' => false

It solved everything. 它解决了一切。

To help you if you have the same problem, below is my builder: 如果遇到同样的问题,为了帮助您,下面是我的构建器:

$builder->add(
    'aboutContents',
    'collection',
    [
        'type' => new AboutContentType(),
        'allow_add' => true,
        'allow_delete' => true,
        'cascade_validation' => true,
        'by_reference' => false,
    ],
    [
        'label' => 'Texts'
    ]
);

Your new AboutContent objects don't know about created About entity, because form: 您的新AboutContent对象不知道创建的About实体,因为表单:
- runs addContent method on About object, - 在About对象上运行addContent方法,
- doesn't run setAbout on AboutContent . - 不在setAbout上运行AboutContent

You have to update the association-field on the owning side ( AboutContent ) to persist the relation. 您必须更新拥有方( AboutContent )上的关联字段以保持关系。

Look at definition of Association Updates . 查看关联更新的定义。

In your code: 在你的代码中:
- The owning side is AboutContent entity (because it has the relation to About ) - 拥有方是AboutContent实体(因为它与About
- The inverse side is About entity - 反面是About实体

So in addContent method you must set About for added AboutContent . 因此,在addContent方法中,您必须为添加的AboutContent设置About

class About {
    //...

    /**
     * Add content
     *
     * @param AboutContent $content
     * @return About
     */
    public function addContent(AboutContent $content)
    {
        $this->content[] = $content;

        $content->setAbout($this); // set the relation on the owning-side

        return $this;
    }

    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM