编程语言
首页 > 编程语言> > php – Symfony2 OneToMany embeded表单不保存实体

php – Symfony2 OneToMany embeded表单不保存实体

作者:互联网

我在Symfony2中遇到OneToMany嵌入式表单的问题.

这次,实体被保存,但不是对父类的引用.

我的桌子看起来像那样

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

我无法理解为什么这个about_id仍然是NULL.

我的关系:

实体关于:

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

我的内容:

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

我的控制器已由我的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(),
    ));
}

我的表单构建器:

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

 }

非常感谢您找到解决方案.

解决方法:

大家好,经过搜索,我终于找到了答案.
问题不是来自我的实体或我的关系船.
它来自我的表格.

在我的类型中,我只是省略了添加标记:’by_reference’=>假

它解决了一切.

如果遇到同样的问题,为了帮助您,下面是我的构建器:

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

标签:php,doctrine,symfony,many-to-one
来源: https://codeday.me/bug/20190717/1490171.html