Many-to-many relationship with refclass attributes

I am currently developing a website using symfony (1.2) with Doctrine as ORM.

I have a Dinner class, a Criteria class and a Mark class.

  • The Sign is associated with Dinner and Criteria and has private attributes like DateOfMark, MarkValue, etc.
  • Dinner and Criteria can have many Signs (or not).

Since I am using Doctrine, I am defining 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

      

The problem is that the SQL generated by Doctrine adds FOREIGN KEY CONSTRAINT

on Mark.dinner_id

to Dinner.id

(which is correct) AND adds FOREIGN KEY CONSTRAINT

on Dinner.id

to Mark.dinner_id

(which is indeed incorrect, since Dinner can have many characters).

Question

Did I miss something? Am I doing this kind of relationship between classes wrong?

Thank.

+2


source to share


1 answer


You need to define the relationship as a one-to-many relationship. 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

      

+3


source







All Articles