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

发布于 2024-11-02 17:34:20 字数 1011 浏览 0 评论 0原文

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

所以,开门见山。 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)。

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

尝试将“partners”和“partner_list”放入apps/backend/modules/user/generator.yml中(我成功添加了一个布尔值和一个静态选择[通过*Peer:: getXXXChoices()] 字段已存在),结果却出现错误“Widget 'partners' does not exit”。

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

那么我该怎么做呢?如果我确实遗漏了一些重要的内容,请随时询问更多细节。

问候。

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.)

So, straight to the point. The 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).

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).

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".

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.

Regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陌伤ぢ 2024-11-09 17:34:20

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

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

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

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

因此,如果您确实希望为您生成 symfony 创建的多选及其背后的所有逻辑,则需要在这两个类之间创建多对多关系。模式最终应如下所示:

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 小部件。我总是取消设置我不需要的那个,而且真的很有魅力。

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

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 }

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 ).

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}

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文