在 symfony 中更新外部实体时,多对多关系被删除
我正在使用 Symfony 1.2(无法更新,这不取决于我),使用 Propel 作为 ORM。我的多对多关系遇到了奇怪的问题。
假设您有两个表 A
和 B
,以及一个多对多关系 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)
关系的情况下更新 a
和 b
。
所有表都是 MySQL/InnoDB。 A_has_B
中的 A_id
和 B_id
列是指向 A
和 B
id 的外键。我在两列中都有 ON DELETE CASCADE
和 ON UPDATE CASCADE
。
非常感谢您的帮助。
更新:这是 yml 架构,用于三个表 Team
和 Participants
及其关系
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,我意识到问题是什么了。当然,有些愚蠢的事情。
在我的关系中,假设
A_has_B
我隐藏了B
Generator.yml 中的a_has_b_list
字段,只允许从A
进行修改> 形式。但这是一个错误,因为仅隐藏generator.yml中的字段并不会取消它的设置,只是隐藏它并以空白值发布(因此它会覆盖数据库)。有必要在B
表单configure()
方法中取消设置小部件:这样,不会发布任何内容,并且不会覆盖数据库。
Ok, I realized what the problem was. Of course, something silly.
In my relations, say
A_has_B
I was hidding thea_has_b_list
field inB
generator.yml to only allow modifications fromA
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 inB
formconfigure()
method:This way, nothing is posted and database is not overriden.