管理表单中的一对多关系 [英] Struggling with one-to-many relation in an admin form

查看:38
本文介绍了管理表单中的一对多关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的问题的答案很明显,我深表歉意.我是 Symfony 的新手,我无法在 SO 找到与我的问题完全相同的另一个问题,因此我发布了一个问题.(而且 Google 也没有多大帮助,但我对 Symfony 的术语又不是很熟悉,所以我的查询措辞可能很糟糕.)

I do apologize if the answer to my question would be very obvious. I am new to Symfony and I wasn't able to find another question with exactly the same issue as mine at SO, thus I am posting a question. (And Google didn't help much either, but then again I am not very familiar with Symfony's terminology, so I might have worded my queries badly.)

所以,直奔主题.schema.yml:

user:
  id:
  email:   { type: varchar, size: 255, required: true }
  ... # etc.

partner:
  user_id: { type: integer, foreignTable: user, foreignReference: id }

(顺便说一句,使用 Symfony 1.3 和 Propel 1.4).

(BTW, using Symfony 1.3 and Propel 1.4).

所以我生成了 $user->getPartners()$partner->getUserId() 方法(即使我在某处读到,如果您的 FK 是 PK在引用的表中,Propel 强制建立一对一关系,但我观察到一对多关系,除非我弄错了).美好的.但是,我有一个管理模块来编辑用户,目前我什至难以理解我究竟如何让 Symfony 在用户/编辑"表单中显示多选合作伙伴列表(双列表就可以了也是).

So I have $user->getPartners() and $partner->getUserId() methods generated (even though I read somewhere that if your FK is a PK in the referenced table, Propel forces one-to-one relationship, but I observe one-to-many, unless I got it very wrong). Fine. However, I have an admin module to edit an User and at the moment I am struggling to even understand how exactly am I to make Symfony show a multiple-select list of Partners in the "User/edit" form (double list would be fine too).

尝试在 apps/backend/modules/user/generator.yml 中放入partners"和partner_list"(我成功地添加了一个布尔值和一个静态选择 [通过 *Peer::getXXXChoices()] 字段),只会得到错误小工具‘合作伙伴’不存在".

Tried with putting "partners" and "partner_list" in apps/backend/modules/user/generator.yml (where I successfully added a boolean and a static-choice [via *Peer::getXXXChoices()] fields already), only to get errors "Widget 'partners' doesn't exist".

我想我可以去编辑表单类,但我不知道如何告诉 Propel 使用multiple = true"来形成一对多的视觉关系,因为选择"不是静态的;这取决于另一个表.

I could go edit the form class I guess, but I have no idea how to tell Propel to form a one-to-many visual relationship using "multiple = true", because "choices" isn't static; it depends on another table.

那我该怎么做呢?如果我确实遗漏了一些重要的内容,请随时询问其他详细信息.

So how do I do this? Feel free to ask for additional details if I did omit something crucial.

问候.

推荐答案

我曾经遇到过同样的问题,所以这是我的解决方案.Symfony 在这种情况下不是很聪明,所以你需要帮助它.您描述的模型是对问题的完美关系模型解释:

I had come across the same problem once so this is my solution to the problem. Symfony is not very smart in this kind of situations, so you need to help it a bit. The model you describe is perfeclty good relational model interpretation of the problem:

user:
  id:
  email:   { type: varchar, size: 255, required: true }
  ... # etc.

partner:
  user_id: { type: integer, foreignTable: user, foreignReference: id }

事实上,Symfony 确实简化了它,当 Propel 生成表单并且生成器解析提交表单以保存数据时,它解释 user_id 字段就像它是任何普通字段一样(不是以一对多的方式).

Thing is that Symfony really simplifies it and when Propel generates forms and generator parses submition forms in order to save data, it interprets the user_id field like if it was any normal field (not in a one-to-many way ).

因此,如果您真的希望为您生成由 symfony 创建的多选及其背后的所有逻辑,则需要在这两个类之间创建多对多关系.架构应该是这样的:

So if you really want that multiselect created by symfony and all logic behind it to be generated for you, you will need to create a many to many relation between those two classes. The schema should end up something like this:

user:
  id:
  email:   { type: varchar, size: 255, required: true }
  ... # etc.

partner:
  user_id: { type: integer, foreignTable: user, foreignReference: id }

user_partner:
  user_id: { type: integer , foreignTable: user, foreignReference: id, primaryKey: true}
  partner_id: { type: integer , foreignTable: partner, foreignReference: id, primaryKey: true}

通过这种方式,您将在用户表单中拥有 partner_list 小部件,在合作伙伴表单中也将拥有 user_list 小部件.我总是取消设置我不需要的那个,而且真的很有魅力.

This way you will have the partner_list widget in the user form and also the user_list widget in the partner form. I always unset the one i dont need and really works like a charm.

其他解决方案,最适合您的模型实现的解决方案有点复杂,因为您需要修改 UserForm 并添加一个 partner_ids 多选小部件,然后修改 doSave 方法以能够处理多选保存逻辑(创建实例,将它们关联到用户并保存它们,同时删除未选中的实例).

The other solutions, the one that fits best your model implementation is a bit more complex becase you'll need to modify the UserForm and add a partner_ids multiselect widget then modify the doSave method to be able to handle the multiselect save logic (create instances , associate them to the user and save them, also remove the ones not selected).

这篇关于管理表单中的一对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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