简体   繁体   English

Symfony + doctrine +一对多的关系

[英]Symfony + doctrine + one-to-many relationships

I need a one-to-many relationships for my sf 1.4 + doctrine 1.2 project. 我的sf 1.4 + doctrine 1.2项目需要一对多的关系。 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. 然后,我重建我的架构,并为User和Phonenumber生成后端模块。

In the Phonenumber admin panel, I can set the user by a select box with Users. 在Phonenumber管理面板中,我可以通过包含用户的选择框设置用户。 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? 我怎样才能在这里添加许多关系?

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.... 我更喜欢这样做,因为你的用户很可能会有很多关系,而不是phonenumbers,所以这一切都在一个地方....

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. 鉴于您要为两个表声明“id”而不是允许Doctrine自动创建它们,您可能需要包含“本地”和“外部”...未经测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM