[Symfony1.4] How to Create Multiple Relations to Columns in the Same Table

Tadashi Shigeoka ·  Tue, November 15, 2011

When creating schema.yml in Symfony1.4, I struggled with setting up multiple relations to columns in the same table, so here’s a memo.

To store information about the user who registered the book information and the user who modified it, I have registry_user_id and edit_user_id records in the Book table.

I want each of these to have a relation with the id in the User table, but when I write it as follows, only the latter edit_user_id can have the relation.

Book:
  actAs:
    Timestampable: ~
    Sluggable: ~
  columns:
    .
    .
    registry_user_id:
      type:             integer
      notnull:          true
    edit_user_id:
      type:             integer
    .
    .
  relations:
    User:
      onDelete:         CASCADE
      local:            registry_user_id
      type:             one
      alias:            RegistryUser
      foreign:          id
      foreignType:      one
    User:
      onDelete:         CASCADE
      local:            edit_user_id
      type:             one
      alias:            EditUser
      foreign:          id
      foreignType:      one

To solve this, instead of directly specifying the class name, give each relation a name and specify class: User like below:

    RegistryUser:
      onDelete:         CASCADE
      class:            User
      local:            registry_user_id
      type:             one
      foreign:          id
      foreignType:      one
    EditUser:
      onDelete:         CASCADE
      class:            User
      local:            edit_user_id
      type:             one
      foreign:          id
      foreignType:      one

That’s all from the Gemba.