在 symfony 中更新外部实体时,多对多关系被删除

发布于 2024-12-23 06:00:11 字数 2002 浏览 1 评论 0原文

我正在使用 Symfony 1.2(无法更新,这不取决于我),使用 Propel 作为 ORM。我的多对多关系遇到了奇怪的问题。

假设您有两个表 AB,以及一个多对多关系 A_has_B。假设我在 A 中有一个 a 实体,在 B 中有一个 b 实体,在 B 中有一个关系>A_has_B,PK 为(a_id, b_id)。现在,如果我使用 symfony 管理生成的模块更新 b 实体(不是其 id,而是不是其 PK 的另一个字段),则 (a_id, b_id) 将从中删除数据库。

这仅在使用 Symfony 后端时发生。使用 phpmyadmin 不会发生这种情况,我可以在不丢失 (a_id, b_id) 关系的情况下更新 ab

所有表都是 MySQL/InnoDB。 A_has_B 中的 A_idB_id 列是指向 AB id 的外键。我在两列中都有 ON DELETE CASCADEON UPDATE CASCADE

非常感谢您的帮助。

更新:这是 yml 架构,用于三个表 TeamParticipants 及其关系

propel:
  _attributes:
    package: lib.model
    defaultIdMethod: native
  team:
    _attributes: { phpName: Team }
    id: { type: INTEGER, size: '11', primaryKey: true, autoIncrement: true, required: true }
    name: { type: VARCHAR, size: '255', required: true }
    description: { type: LONGVARCHAR, required: false }
  participant:
    _attributes: { phpName: Participant }
    id: { type: INTEGER, size: '11', primaryKey: true, autoIncrement: true, required: true }
    name: { type: VARCHAR, size: '255', required: true }
  team_has_participant:
    _attributes: { phpName: TeamHasParticipant }
    team_id: { type: INTEGER, size: '11', primaryKey: true, required: true, foreignTable: team, foreignReference: id, onDelete: CASCADE, onUpdate: CASCADE }
    participant_id: { type: INTEGER, size: '11', primaryKey: true, required: true, foreignTable: participant, foreignReference: id, onDelete: CASCADE, onUpdate: CASCADE }
    _indexes: { participant_id: [participant_id] }

假设我更新 description 字段在 team 实体中,那么我将失去拥有该外部 team 实体的所有 team_has_participant 关系。

I'm working with Symfony 1.2 (can't update, it's not up to me) with Propel as ORM. I'm having a bizarre problem with my many-to-many relations.

Let's say you have two tables A and B, and a many-to-many relation A_has_B. Let's say I have an a entity in A, a b entity in B, and a relation in A_has_B which PK is (a_id, b_id). Now, if I update b entity (not its id, but another field which is not its PK) using symfony admin generated modules, then (a_id, b_id) is dropped from the database.

This only happens using Symfony backend. It DOESN'T happen using phpmyadmin, where I can update a and b without loosing (a_id, b_id) relation.

All tables are MySQL/InnoDB. Columns A_id and B_id in A_has_B are foreign keys pointing to A and B id's. I have ON DELETE CASCADE and ON UPDATE CASCADE in both columns.

Thank you very much for your help.

UPDATE: Here is the yml schema, for three tables Team and Participants and its relation

propel:
  _attributes:
    package: lib.model
    defaultIdMethod: native
  team:
    _attributes: { phpName: Team }
    id: { type: INTEGER, size: '11', primaryKey: true, autoIncrement: true, required: true }
    name: { type: VARCHAR, size: '255', required: true }
    description: { type: LONGVARCHAR, required: false }
  participant:
    _attributes: { phpName: Participant }
    id: { type: INTEGER, size: '11', primaryKey: true, autoIncrement: true, required: true }
    name: { type: VARCHAR, size: '255', required: true }
  team_has_participant:
    _attributes: { phpName: TeamHasParticipant }
    team_id: { type: INTEGER, size: '11', primaryKey: true, required: true, foreignTable: team, foreignReference: id, onDelete: CASCADE, onUpdate: CASCADE }
    participant_id: { type: INTEGER, size: '11', primaryKey: true, required: true, foreignTable: participant, foreignReference: id, onDelete: CASCADE, onUpdate: CASCADE }
    _indexes: { participant_id: [participant_id] }

Say I update description field in a team entity, then I'm loosing all the team_has_participant relations that had that foreign team entity.

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

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

发布评论

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

评论(1

神仙妹妹 2024-12-30 06:00:11

好吧,我意识到问题是什么了。当然,有些愚蠢的事情。

在我的关系中,假设 A_has_B 我隐藏了 B Generator.yml 中的 a_has_b_list 字段,只允许从 A 进行修改> 形式。但这是一个错误,因为仅隐藏generator.yml中的字段并不会取消它的设置,只是隐藏它并以空白值发布(因此它会覆盖数据库)。有必要在 B 表单 configure() 方法中取消设置小部件:

unset($this->widgetSchema['a_has_b_list'];

这样,不会发布任何内容,并且不会覆盖数据库。

Ok, I realized what the problem was. Of course, something silly.

In my relations, say A_has_B I was hidding the a_has_b_list field in B generator.yml to only allow modifications from A form. But it's a mistake, because just hidding the field in generator.yml doesn't unset it, just hidde it and it is posted with a blank value (so it overrides the database). It's necessary to unset the widget in B form configure() method:

unset($this->widgetSchema['a_has_b_list'];

This way, nothing is posted and database is not overriden.

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