Naming a relation in Doctrine 2 ORM?

后端 未结 2 1943
野的像风
野的像风 2021-02-06 01:07

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\\         


        
相关标签:
2条回答
  • 2021-02-06 01:51

    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 讨论(0)
  • 2021-02-06 02:09

    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.

    0 讨论(0)
提交回复
热议问题