[英]Symfony2 embedded form OneToMany relationship, how to persist related data
[英]Symfony Form with Sub Form and OneToMany Relationship
存在一個主題實體,它可以具有許多注釋實體,一對多。
創建表單TopicType時,將嵌入表單CommentType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title')
->add('comments', CommentType::class)
;
}
Symfony使用標題輸入框和評論輸入框(例如消息)正確呈現此表單。
提交表單時發生錯誤:
Neither the property "messages" nor one of the methods
"addMessag()"/"removeMessag()", "addMessage()"/"removeMessage()",
"setMessages()", "messages()", "__set()" or "__call()" exist and have
public access in class "Foo\BarBundle\Entity\Topic".
將評論表嵌入主題表的正確方法是什么? 用戶只能在創建主題時添加一個評論。
應該使用CollectionType
嗎? 這將導致在顯示“注釋”之前將0渲染為表單標簽。 (0是附加到Thread的新Comment()的第一個索引)。
->add('messages', CollectionType::class)
您確實需要一個CollectionType::class
,然后您將需要一些JavaScript以便能夠從同一表單添加多個注釋。 該文檔說明了如何執行此操作。
http://symfony.com/doc/current/cookbook/form/form_collections.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.