gpt4 book ai didi

symfony - 如何在 Sonata Admin 中正确配置 'sonata_type_collection' 字段

转载 作者:行者123 更新时间:2023-12-02 06:58:16 24 4
gpt4 key购买 nike

简而言之:

当我在 OneToMany 关系中使用“sonata_type_collection”时,我必须指定关系的另一端,在“创建操作”中仍然不存在,并且可以在“更新操作”中设置,但也可以指定完全不同的父级。

更详细的解释:

我正在使用 Sonata Admin Bundle 进行 CRUD 操作,假设我只有 Post(id, title, content) 和 Tag(id, post_id, title) 实体。我希望能够在编辑 Post 实体时添加和删除标签实体,因此我使用“sonata_type_collection”字段。

这是 PostAdmin 类中的 configureFormFields 方法:

protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('content')
->add('tags', 'sonata_type_collection', array(), array(
'edit' => 'inline',
'inline' => 'table'
))
))
;
}

问题是在创建表单中,当我添加新标签时,我必须同时指定帖子和标题,但帖子仍然不存在,所以我无法添加标签。在编辑帖子时,我可以添加新标签,但对于每个标签,我都必须明确设置一个帖子,例如,我可以为完全不同的帖子添加标签。

你能告诉我如何解决这个问题吗?

最佳答案

您可能希望将 by_reference 选项设置为 false。

protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('title')
->add('content')
->add('tags', \Sonata\CoreBundle\Form\Type\CollectionType::class,
array('by_reference' => false),
array('edit' => 'inline',
'inline' => 'table'
)
);
}

[编辑]所以看起来问题来自 Post 实体,它必须从 addTag() 方法调用标签的 setPost() 方法。

public function addTag($tag)
{
$tag->setPost($this);
$this->tags->add($tag);

return $this;
}

关于symfony - 如何在 Sonata Admin 中正确配置 'sonata_type_collection' 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26890310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com