【发布时间】:2012-10-31 22:27:20
【问题描述】:
我有一个带有标签字段的表单。 Tags 是绑定实体上的 Doctrine ArrayCollection。正如文档建议的那样,该字段是 by_reference=false ,但是在添加新元素时将表单绑定到实体是非法行为,如下所示:
$data=$entity->getTags(); //gets the ArrayCollection but does not care that it is not an array, and shoulrd be converted first
//do the value modifications like:
$data[]=new Tag(...);
$entity->setTags($data); //poor setter gets called with the already-updated collection, this operation is pointless
我认为 by_reference false 是为了避免这个问题。如果是,那么它出现故障。如果不是,那么文档很差,有一个 ArrayCollections 的例子,但不关心这种对设置器的非常残酷的忽视......
我应该改用什么?在 getter 中返回 toArray() 是行不通的(显然设计模型以兼容较差的表单实现是不明智的。是否可能有类似于“集合”的类型强制转换为数组?
【问题讨论】:
-
对不起,我可能一直在混淆:问题中的代码旨在说明 FormType 的行为方式,它不是我的代码。除了 $form->bindRequest(...) 和字段定义之外,我真的没有这个特定方面的代码。
-
2.0(我想移植到 2.1,但现在买不起)。
标签: symfony collections arraycollection fieldtype