与 refClass 中的属性的多对多关系

发布于 2024-08-08 10:32:16 字数 1344 浏览 5 评论 0原文

我目前正在设计一个网站,使用 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 技术交流群。

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

发布评论

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

评论(1

墨洒年华 2024-08-15 10:32:17

您需要将关系定义为一对多关系。试试这个(注意晚餐和标准定义中添加的“类型:很多”):

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
      type: many

Criteria:
  columns:
    name: { type: string(50), notnull: true }
 relation:
   Marks:
     class:    Dinner
     local:    criteria_id
     foreign:  dinner_id
     refClass: Mark
     type: many

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

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

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
      type: many

Criteria:
  columns:
    name: { type: string(50), notnull: true }
 relation:
   Marks:
     class:    Dinner
     local:    criteria_id
     foreign:  dinner_id
     refClass: Mark
     type: many

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