【问题标题】:Symfony2 OneToMany embeded form doesn't save entity onSymfony2 OneToMany 嵌入表单不保存实体
【发布时间】:2015-02-03 13:58:17
【问题描述】:

我在 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'))       
 ;

 }

如果您找到解决方案,非常感谢。

【问题讨论】:

    标签: php symfony doctrine many-to-one


    【解决方案1】:

    您的新 AboutContent 对象不知道创建的 About 实体,因为表单:
    - 在 About 对象上运行 addContent 方法,
    - 不会在 AboutContent 上运行 setAbout

    您必须更新拥有方 (AboutContent) 的关联字段以保持关系。

    查看Association Updates的定义。

    在您的代码中:
    - 拥有方是AboutContent 实体(因为它与About 有关系)
    - 反面是About实体

    所以在addContent 方法中,您必须设置About 以添加AboutContent

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

    【讨论】:

    • 谢谢,我试过了,但没有任何改变
    【解决方案2】:

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

    在我的类型中,我只是省略了添加标签:'by_reference' => false

    它解决了一切。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多