Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony4 Doctrine2 custom NamingStrategy how to

I am just starting with Symfony4 / Doctrine2. So, I need to implement Doctrine2 custom NamingStrategy but cannot find any documentation/samples besides this one: https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/namingstrategy.html and this answer How to setup table prefix in symfony2. They do not explain much in terms how to do this in relations to the Symfony4. I understand that I need to create a class that implements NamingStrategy interface, so where should I put this class in the Symfony4 folder structure, in the src/Entity?

Then I need to register the class as a service, it must be easy for a seasoned Symfony developer to know, but as I am just starting, would be nice to have some elaboration on this. Thanks, any input is appreciated.

like image 599
Valery Vidershpan Avatar asked Oct 27 '25 03:10

Valery Vidershpan


1 Answers

Anyway, I have managed to figure it out myself.

First I have created custom naming strategy class extending UnderscoreNamingStrategy. I have placed it under src/Service/, still not sure if this is the right place for it to go to in the folder hierarchy.

Then I have registered this class as a service by adding following to the end of the the config/services.yaml :

...

# Naming Strategy
app.naming_strategy.custom:
    class: App\Service\CustomNamingStrategy
    autowire: true

After that I have configured the doctrine to use this custom naming strategy by editing config/packages/doctrine.yaml as follows:

I have commented out:

    naming_strategy: doctrine.orm.naming_strategy.underscore

and added under it:

    naming_strategy: app.naming_strategy.custom
like image 167
Valery Vidershpan Avatar answered Oct 29 '25 07:10

Valery Vidershpan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!