简体   繁体   English

在管理表单中处理一对多关系

[英]Struggling with one-to-many relation in an admin form

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. 我是Symfony的新手,我无法在SO中找到与我的问题完全相同的另一个问题,因此我要发布一个问题。 (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.) (谷歌也没有太大帮助,但是我又对Symfony的术语不太熟悉,所以我的措辞可能不好。)

So, straight to the point. 因此,直截了当。 The schema.yml : schema.yml

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

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

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

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). 因此,我生成了$ user-> getPartners()$ partner-> getUserId()方法(即使我在某处读到,如果您的FK是引用表中的PK,Propel也会强制一对一关系,但是我观察到一对多,除非我弄错了。 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). 但是,我有一个管理模块来编辑用户,此刻,我甚至在努力地理解我如何使Symfony以“用户/编辑”形式显示合作伙伴的多选列表(最好使用双列表)太)。

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". 尝试将“伙伴”和“伙伴列表”放在apps / backend / modules / user / generator.yml中 (在这里我已经成功地通过[ Peer :: getXXXChoices() ]添加了一个布尔值和一个静态选择),仅用于收到错误“窗口小部件“伙伴”不存在”。

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; 我可以去编辑我猜的表单类,但是我不知道如何告诉Propel使用“ multiple = true”形成一对多的视觉关系,因为“选择”不是一成不变的。 it depends on another table. 它取决于另一个表。

So how do I do this? 那么我该怎么做呢? Feel free to ask for additional details if I did omit something crucial. 如果我确实省略了一些重要的事情,请随时询问其他细节。

Regards. 问候。

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. 在这种情况下,Symfony不太聪明,因此您需要一点帮助。 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 }

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确实简化了它,并且当Propel生成表单并生成器解析提交表单以保存数据时,它会解释user_id字段,就像它是任何普通字段一样(不是一对多的方式)。

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. 因此,如果您确实希望由symfony创建的multiselect及其背后的所有逻辑都可以为您生成,则需要在这两个类之间创建多对多关系。 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}

This way you will have the partner_list widget in the user form and also the user_list widget in the partner form. 这样,您将在用户表单中拥有partner_list小部件,在合作伙伴表单中还将具有user_list小部件。 I always unset the one i dont need and really works like a charm. 我总是取消我不需要的那个,并且确实像一种魅力。

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). 其他解决方案(最适合您的模型实现的解决方案)要复杂一些,因为您需要修改UserForm并添加partner_ids multiselect小部件,然后修改doSave方法以能够处理multiselect保存逻辑(创建实例,将其与用户相关联并保存,并删除未选中的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM