在Doctrine 2 ORM中命名关系?

时间:2011-10-02 00:59:28

标签: symfony doctrine doctrine-orm yaml

如何使用YAML为多对一关系“region”设置外键的名称(编辑:不是属性本身的名称)?

SWA\TestBundle\Entity\Province:
  type: entity
  table: province
  uniqueConstraints:
    UNIQUE_PROVINCE_CODE:
      columns: code
  id:
    id:
      type: integer
      generator: { strategy: AUTO }
  fields:
    code:
      type: integer
    name:
      type: string
      length: 255
    short_name:
      type: string
      length: 2
  manyToOne:
    region:
      targetEntity: Region
      inversedBy: provinces

2 个答案:

答案 0 :(得分:11)

查看getCreateConstraintSQL method in the AbstractPlatform class以了解如何选择外键名称(line 1088)。

直接从约束名称中获取。影响约束名称将影响外键名称。

作为一种解决方法,您可以删除约束并使用a doctrine migration中的新名称重新创建约束。

答案 1 :(得分:1)

由于@JakubZalas的回答,我看了一下Github中的代码,并且已经看到改变框架代码以实现你想要的东西非常简单。

如果检查AbstractPlatform类所在的文件夹,您将看到有一个ForeignKeyConstraint类。在它中你会看到它继承自AbstractAsset。

现在AbstractAsset类有一个_generateIdentifierName方法。如果你在github 中检查这个方法,你会发现它有一个注释部分可以满足你的需要。您只需取消注释此部分,注释实际的活动部分,将$ prefix参数更改为$ postfix,您就完成了。将使用具有相应后缀的表名和列名生成约束名称。

AbstractAsset.php文件是这个文件夹:Symfony / vendor / doctrine / dbal / lib / Doctrine / DBAL / Schema

我试过我的项目蚂蚁它工作得很好。

最后一条信息:至少在我的项目中,我上面提到的评论部分仅在github中,而不在我本地机器的文件中。