Symfony CollectionType更新实体 [英] Symfony CollectionType update Entities

查看:35
本文介绍了Symfony CollectionType更新实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不敢相信这对于其他人来说还没有实现,但是我找不到解决方案.

I can't believe this hasn't come up for other people, but I'm unable to find a solution.

比方说,我有两个实体类型,具有一对多关系的A和B.A具有B的集合.

Let's say I have two entity types, A and B with a one-to-many relationship. A has a collection of Bs.

A的表单具有B的 CollectionType ,具有针对B的自定义 entry_type allow_add allow_delete 设置为 true .创建/填充/呈现表单时,B的字段由它们在集合中的索引标识.表单回发后,这些字段会再次根据索引重新映射到B实体上.

The form for A has a CollectionType for the Bs, with a custom entry_type for B, allow_add and allow_delete set to true. When the form is created/populated/rendered, the Bs' fields are identified by their index in the collection. When the form is posted back, the fields are mapped back onto the B entities according to the index again.

如果数据库同时决定按不同顺序返回B,该怎么办?然后在Bs上交换值!我不能拥有它,因为其他实体会引用B,而现在它们已经改变了含义!

What if the database in the mean time decided to return the Bs in a different order? Then the values get swapped around on the Bs! I can't have that, as other entities will reference the Bs and now they've changed their meaning!

即使数据库没有更改顺序,删除B时也会出现相同的问题:字段在B中移动,而另一个B被删除!(好吧,我不是100%肯定会发生这种情况,因为在已发布字段的编号中存在间隙.)我发现了类似的问题,当创建另一个字段时,它确实会发生(Symfony CollectionType将删除+创建视为对项的修改),但从问题上可以看出,并没有可用的答案.

Even if the database doesn't change the order, the same issue appears when a B is deleted: The fields get shifted through the Bs and a different one deleted! (Ok, I'm not a 100% certain this happens, as there's a gap then in the numbering of the posted fields.) I've found this similar question where it does happen when another one is created (Symfony CollectionType regards deletion+creation as a modification of an item), but that sort of drifted from the issue and there's no usable answer.

如何确定表单正在更新用户实际编辑的实体?

我已经尝试将B的ID呈现为 HiddenType ,但是随后该表格正确地抱怨ID没有设置器.无论如何,这可能会在错误的B上强加一个ID,而Doctrine并不喜欢这样.我想我可以将B添加为未映射对象,然后手动将这些值复制到正确的对象上,但这会破坏Symfony表单系统的一大块.

I already tried to render the Bs' IDs as a HiddenType, but then the form rightfully complains that the ID has no setter. It would probably force an ID on the wrong B anyways and Doctrine doesn't like that. I suppose I could add the Bs as unmapped and copy the values over to the correct objects manually, but that would defeat a good chunk of Symfony's form system.

我以前使用过 CollectionType ,但没有用于其他地方引用的实体.然后,我将删除所有先前的实体,并根据发布的数据重新创建集合.但是我现在不能这样做,可以吗?

I've used CollectionType before, but not for entities that are referenced elsewhere. I would then delete all of the previous entities and create the collection anew from the posted data. But I can't do that now, can I?

推荐答案

自学说2.1开始,可以更改

Since doctrine 2.1, it's possible to change how associations are indexed. This will allow you to use the id as the collection key (as the field has to be unique):

@OneToMany(targetEntity="B", mappedBy="A", indexBy="id")

您可能还需要启用 orphanRemoval ,以便实际上删除数据,而不是仅仅将关系设置为 null .

这篇关于Symfony CollectionType更新实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆