我有一個物體教室,里面有很多學生物體,一個學生只屬于 1 個教室
我的課堂形式:
$builder
->add('name')
->add('students', CollectionType::class, [
'entry_type' => StudentType::class,
'allow_add' => true,
'allow_delete' => true,
])
;
添加新學生時,課堂學生集合有新添加的學生,而新學生欄位課堂為NULL,我在我的課堂物體上添加了事件級聯持久性
@ORM\OneToMany(targetEntity=Student::class, mappedBy="classroom", orphanRemoval=true, cascade={"persist"})
但我仍然收到新學生的課堂欄位為 NULL 的錯誤
Column 'classroom_id' cannot be null
有誰知道為什么?
uj5u.com熱心網友回復:
實際上 symfont 不會呼叫您的方法addStudent,因為默認情況下該選項by_reference具有true,因此要強制 symfony 使用您的方法,您必須設定by_reference為false.
->add('students', CollectionType::class, [
'entry_type' => StudentType::class,
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
])
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/331937.html
