Symfony2 - 使用实体数据库 ID 作为表单集合数组键 [英] Symfony2 - Using the Entities database ID for Form Collections array key
问题描述
我正在构建一个表单集合,它输出的行如下:
I am building a form collection and it output's the rows like this:
<input type="number" name="order[items][1][qty]">
<input type="number" name="order[items][2][qty]">
但我想做的是有这个:
<input type="number" name="order[items][DB_ID_1][qty]">
<input type="number" name="order[items][DB_ID_2][qty]">
DB_ID_1 将是数据库记录 ID 的数值.所以我可以像这样打印模板中的每一行:
DB_ID_1 would be a numeric value of the database record ID. So I can print out each row in my template like this:
{{ form.items.1 }}
所以它打印出 ID 为 1 的数据库记录的表单元素,当前它打印出键为 1 的元素,例如可能具有 9 的真实数据库 id
So it prints the form element out for the database record with ID 1, currently it prints out the element for the key of 1 which could have the real database id of 9 for example
需要更多信息,请告诉我.谢谢
Any more information required please let me know. Thanks
推荐答案
我终于在这篇文章的帮助下找到了答案嵌入表单集合 Symfony2表单允许添加和删除
I have finally found the answer with the help of this post Embedding a collection of forms Symfony2 forms with adding and deleting allowed
这是一个如此简单的解决方案 - 我找错了地方,我需要在我的自定义类存储库中使用 INDEX BY x.id,如下所示:
It was such a simple solution - I was looking in the wrong place, I needed to use INDEX BY x.id in my custom Class Repository like so:
$em->createQuery("SELECT f FROM ExampleCoreBundle:Folder f INDEX BY f.id WHERE f.uid = :uid");
这返回了我的 Folder 对象数组,数组的键是文件夹的数据库记录的 id 字段.
This returned my array of Folder objects back with the keys of the array being the database record's id field for the folder.
这现在可以很好地与我最初想要的表单集合配合使用 - 我很确定此信息不在网站的表单集合"页面上,所以希望这对将来的其他人有所帮助!
This now works fine with the Form Collections as I originally wanted - I am pretty sure this information is not on the Form Collections page on the website so hope this helps anyone else in future!
这篇关于Symfony2 - 使用实体数据库 ID 作为表单集合数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!