【问题标题】:Doctrine/MySql relationship with a multi-column PK table多列PK表的Doctrine/MySql关系
【发布时间】:2012-06-18 09:46:59
【问题描述】:

我在 schema.yml (Symfony 1.4) 中的 Post 模型如下所示:

Post:
  columns:
    id: { type: bigint, notnull: true, primary: true }
    blog_id: { type: bigint, notnull: true, primary: true }
    user_id: { type: bigint, notnull: true }
    subject: { type: string(255), notnull: true }
    short_body: { type: text, notnull: true }
    long_body: { type: text }

如您所见,Post 有一个多列 PK。我的问题是“我怎样才能与这个模型建立 n:1 关系?”

例如,我想要这样的东西:

PostComment:
  columns:
    post_id: { type: bigint, notnull: true }
    blog_id: { type: bigint, notnull: true }
    name: { type: string(255), notnull: true }
    email: { type: string(255) }
    text: { type: text, notnull: true }
  relations:
    Post:
      #Here is my problem. What should I write here?
      local: ????
      foreign: ????
      foreignAlias: Comments
      onDelete: cascade
      onUpdate: cascade

我该如何处理这种类型的关系?

【问题讨论】:

  • 我不熟悉这种表示法,但您需要一对多。我猜postcomment也应该有它的ID!

标签: php mysql symfony1 doctrine symfony-1.4


【解决方案1】:

你不能。 您可以向 Post 模型添加另一个主键,并保持另外几个字段是唯一的。如果您这样做,我强烈建议您将“id”作为您的主键,并将另一个字段名称与“blog_id”一起使用。 然后,像往常一样使用 PostComment 中的关系。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多