嵌入一​​组表单Symfony2表单,添加和删除允许 [英] Embedding a collection of forms Symfony2 forms with adding and deleting allowed

查看:137
本文介绍了嵌入一​​组表单Symfony2表单,添加和删除允许的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Symfony2中,如果我嵌入一组表单,指向一对一的关系在Doctrine并允许添加和删除,如果我从一开始删除一个记录,最后添加一个,并在中间编辑一些系统如何知道哪些记录更新与哪些数据?

In Symfony2, if I embed a collection of forms pointing at a many to one relationship in Doctrine and allow adding and deletion, if I delete a record from the beginning, add one at the end, and edit some in the middle how does the system know which records to update with which data?

教程中没有任何内容传递嵌入式数据的主键。在某些情况下,我的记录被不必要地删除并重新添加,而不是进行编辑(即使特定记录没有更改)。这打破了表单中未包含的记录中的字段,将其从数据库模型设置为默认值。

There is nothing in the tutorial that passes the primary key of the embedded data around. Under certain circumstances, my records are getting needlessly deleted and added again rather than edited in place (even if there are no changes to the particular record). This breaks the fields on the records that are not included on the form, setting them to their default values from the DB model.

有没有办法传递主键在表单中,它有用于在数据恢复时执行更新?

Is there a way to pass the primary key in the form and have it used to perform updates when the data comes back?

推荐答案

如果要索引收藏实体id),您可以在实体类中使用 indexBy 注释。

If you want to index the collection (by the entity id) for all querys, you can simply use the indexBy annotation in your entity class.

/**
 * @ORM\OneToMany(targetEntity="EntityClass", mappedBy="EntityVariable", indexBy="id")
 */
private $collection;

这篇关于嵌入一​​组表单Symfony2表单,添加和删除允许的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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