如何让 Symfony Form 使用底层对象的 addSomething() / removeSomething() 方法?

How to make Symfony Form to use addSomething() / removeSomething() methods of the underlying object?

假设我有 User 个对象,它有 $emails 个字段。用户对象也有 addEmail()removeEmail() 方法。 在我的表单类型 (UserType) 中,我将 CollectionType 用于 $emails 字段。 提交表单后,我希望调用方法 User::addEmail()User::removeEmail() 而不是 User::setEmails()。 可能吗?

是的,这是可能的,您只需在构建表单字段时添加选项 'by_reference' => false。