Naming relationships in ORM doctrine 2?

How can I set the name of the foreign key ( change : not the name of the attribute itself) for the one-two relationship between the "one" 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: targetEntity: Region inversedBy: provinces 
+7
source share
2 answers

Look at the getCreateConstraintSQL method in the AbstractPlatform class to find out how the foreign key name is selected ( line 1088 ).

It is taken directly from the name of the constraint. The effect of the constraint name affects the foreign key name.

As a workaround, you can abandon the restriction and recreate it with a new name in the doctrine transfer .

+11
source

Due to @JakubZalas answer, I looked at the code on Github and saw that changing the code of the framework to accomplish what you want is very simple.

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

The AbstractAsset class now has a _generateIdentifierName method. If you test this method on github , you will see that it has a commented part that does exactly what you want. You simply uncomment this part, comment on 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 the corresponding postfix.

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

I tried in my ant project, it worked fine.

One final piece of information: at least for my project, the comment part I mentioned above is only in github, not in the file on my local machine.

+3
source

All Articles