与 refClass 中的属性的多对多关系
我目前正在设计一个网站,使用 symfony (1.2) 和 Doctrine 作为 ORM。
我有一个晚餐类、一个标准类和一个马克类。
- 马克与晚餐和 Criteria,并且具有私有属性, 像 DateOfMark、MarkValue 等。Dinner
- 和 Criteria 可以有很多 标记(或无标记)。
当我使用 Doctrine 时,我使用以下代码在 schema.yml 中定义此模型:
Dinner:
columns:
date: { type: timestamp, notnull: true }
nb_presents: { type: integer, notnull: true }
relations:
Marks:
class: Criteria
local: dinner_id
foreign: criteria_id
refClass: Mark
Criteria:
columns:
name: { type: string(50), notnull: true }
relation:
Marks:
class: Dinner
local: criteria_id
foreign: dinner_id
refClass: Mark
Mark:
columns:
criteria_id: { type: integer, primary: true }
dinner_id: { type: integer, primary: true }
value: { type: integer, notnull: true }
relations:
Dinner:
local: dinner_id
foreign: id
Criteria:
local: criteria_id
foreign: id
问题是 Doctrine 生成的 SQL,它在 Mark.dinner_id< 上添加了
FOREIGN KEY CONSTRAINT
/code> 到 Dinner.id
(这是正确的)AND 它在 Dinner.id
上添加了 FOREIGN KEY CONSTRAINT
> 到 Mark.dinner_id
(这确实不正确,因为晚餐可能有很多标记)。
问题
我错过了什么吗?我在类之间的这种关系做错了吗?
感谢您。
I'm currently designing a website, using symfony (1.2) with Doctrine as an ORM.
I have a Dinner class, a Criteria class, and a Mark class.
- A Mark is linked with a Dinner and a
Criteria, and has private attributes,
like DateOfMark, MarkValue, etc. - Dinner and Criteria can have many
Marks (or none).
As I use Doctrine, I define this model in my schema.yml, using the following code :
Dinner:
columns:
date: { type: timestamp, notnull: true }
nb_presents: { type: integer, notnull: true }
relations:
Marks:
class: Criteria
local: dinner_id
foreign: criteria_id
refClass: Mark
Criteria:
columns:
name: { type: string(50), notnull: true }
relation:
Marks:
class: Dinner
local: criteria_id
foreign: dinner_id
refClass: Mark
Mark:
columns:
criteria_id: { type: integer, primary: true }
dinner_id: { type: integer, primary: true }
value: { type: integer, notnull: true }
relations:
Dinner:
local: dinner_id
foreign: id
Criteria:
local: criteria_id
foreign: id
Problem is that the SQL generated by Doctrine, it adds a FOREIGN KEY CONSTRAINT
on Mark.dinner_id
to Dinner.id
(which is correct) AND it adds a FOREIGN KEY CONSTRAINT
on Dinner.id
to Mark.dinner_id
(which is really incorrect, as a Dinner might have many Marks).
Question
Did I miss something ? Am I doing this kind of relation between classes wrong ?
Thanks you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要将关系定义为一对多关系。试试这个(注意晚餐和标准定义中添加的“类型:很多”):
You need to define the relation as being a one-to-many relation. Try this (note the "type: many" added to the Dinner and Criteria definitions):