Symfony表单无法保存Many-to-Many关联,手动添加正常
问题描述
我有一个简单的博客表单,其中blogCategories字段为多对多关联。调用persist前,Blog对象已包含blogCategories数据,但执行flush后,关联表中并未生成对应条目,且无任何报错。不过若手动调用$blog->addBlogCategory($blogCategory)传入BlogCategory对象,就能正常保存,这说明关联配置无误。
手动添加后的对象结构如下:
App\Entity\BlogEntry {#628 ▼ -id: null -name: "65730a3121b23" -headline: "test" -content: "<p>stack overflow</p>" -published: true -preview_picture: null -releaseTime: DateTimeImmutable @1702038065 {#1255 ▶} -blogCategories: Doctrine\Common\Collections\ArrayCollection {#629 ▼ -elements: array:4 [▼ 0 => App\Entity\BlogCategory {#1269 ▼ -id: 2 -name: "1869478cc6" -ident: "Betriebsrat" -blogEntry: Doctrine\ORM\PersistentCollection {#1276 ▶} } 1 => App\Entity\BlogCategory {#1273 ▼ -id: 3 -name: "017623d368" -ident: "Gut drauf" -blogEntry: Doctrine\ORM\PersistentCollection {#1272 ▶} } 2 => App\Entity\BlogCategory {#1270 ▼ -id: 4 -name: "13afe00644" -ident: "Heimbeirat" -blogEntry: Doctrine\ORM\PersistentCollection {#1294 ▶} } 3 => App\Entity\BlogCategory {#1304 ▼ -id: 5 -name: "0635ff2d31" -ident: "IT" -blogEntry: Doctrine\ORM\PersistentCollection {#1371 ▶} } ] } -author: Proxies\__CG__\App\Entity\Employee {#472 ▶} }
解决方法
已解决:在表单字段配置中设置by_reference=false即可正常工作(感谢Marleen的提示)。
内容的提问来源于stack exchange,提问作者Pilotzi
相关产品推荐
相关产品推荐

