Multiple primary keys with Doctrine 1 and Symfony 1?

后端 未结 2 1921
一整个雨季
一整个雨季 2021-01-26 06:31

I already know that\'s not possible to work with multiple primary keys in Symfony 1 and Doctrine 1, but do you guys know any good workarounds?

相关标签:
2条回答
  • 2021-01-26 07:12

    A common workaround is to add an auto_increment key to the many-to-many resolver table, and rather than have defining relationships, just make standard foreign key relationships to the tables. As long as you have an index on the two columns performance will be fine.

    0 讨论(0)
  • 2021-01-26 07:16

    Beside many-to-many relationships doctrine1 does not work good with a primary key over multiple colums. But if you want to use many-to-many relationships use it like this:

    BlogPost:
      columns:
        user_id: integer
        title: string(255)
        body: clob
      relations:
        User:
          local: user_id
          foreign: id
          type: one
          foreignType: one
          foreignAlias: BlogPosts
        Tags:
          class: Tag
          foreignAlias: BlogPosts
          refClass: BlogPostTag
          local: blog_post_id
          foreign: tag_id
    
    Tag:
      columns:
        name: string(255)
    
    BlogPostTag:
      columns:
        blog_post_id:
          type: integer
          primary: true
        tag_id:
          type: integer
          primary: true
      relations:
        BlogPost:
          local: blog_post_id
          foreign: id
          foreignAlias: BlogPostTags
        Tag:
          local: tag_id
          foreign: id
          foreignAlias: BlogPostTags
    

    If you DONT want/have to use many-to-many relationship its better to use a unique key over multiple columns.

    0 讨论(0)
提交回复
热议问题