开发者

Naming a relation in Doctrine 2 ORM?

How can i set the name of the foreign key (edit: not the name of the attribute itself) for the many-to-one relation "region" using YAML?

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:
  开发者_如何学C    targetEntity: Region
      inversedBy: provinces


Look at the getCreateConstraintSQL method in the AbstractPlatform class to see how the name of the foreign key is chosen (line 1088).

It is taken directly from the constraint name. Influencing constraint name will influence the foreign key name.

As a workaround you could drop the constraint and re-create it with a new name in a doctrine migration.


Due to @JakubZalas answer, I had taken a look to the code in Github, and have seen that changing the framework code for doing what you want is really easy.

If you check the folder where AbstractPlatform class is, you'll see that there is a ForeignKeyConstraint class. In it you'll see it inherits from AbstractAsset.

Now AbstractAsset class has a _generateIdentifierName method. If you check this method in github you'll see that it has a commented part that does just what you want. You just uncomment this part, comment the actual active part, change the $prefix parameter to $postfix and you're done. The constraint name will be generated using the table and column names with a corresponding postfix.

The AbstractAsset.php file is the the this folder: Symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Schema

I tried in my project ant it worked fine.

One final information: at least for my project the commented part I mention above is only in github, not in the file at my local machine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜