views:

1072

answers:

1

Hi everybody.

I need a one-to-many relationships for my sf 1.4 + doctrine 1.2 project. I copied the relation just like this:

User:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    contact_id:
      type: integer(4)
    username:
      type: string(255)
    password:
      type: string(255)

Phonenumber:
  columns:
    id:
      type: integer(4)
      primary: true
      autoincrement: true
    name:
      type: string(255)
    user_id:
      type: integer(4)
  relations:
    User:
      foreignAlias: Phonenumbers

Then, I rebuild my schema, and generate the backend modules for User and Phonenumber.

In the Phonenumber admin panel, I can set the user by a select box with Users. But in the User admin panel I do not have a list with the phonenumbers to allow users to select multiple phonenumbers to the user. How can I add the many relation here?

Thanks in advance

+3  A: 

I prefer to do it this way round as it's likely that your user will have many relations and not phonenumbers, so this keeps it all in one place....

In your User:

relations:
   Phonenumber:
     class: Phonenumber  // model name for relation
     local: id           // key in this table
     foreign: id         // key in the referenced table
     type: many          // User has MANY phonenumbers
     foreignType: one    // Phonenumber has ONE user
     alias: Phonenumber  // What a user calls Phonenumber
     foreignAlias: User  // What a Phonenumber calls User

Given that you're declaring the "id" for both tables instead of allowing Doctrine to create them automatically, you may need to include the "local" and "foreign"... havent tested.

Tom
I think that foreign reference needs to be 'user_id' but you've got it +1
Cryo
@Cryo... indeed, thanks for the correction.
Tom