Multiple primary keys with Doctrine 1 and Symfony

2019-03-04 07:57发布

问题:

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?

回答1:

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.



回答2:

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.