Search code examples
doctrine-ormgetter-settercamelcasing

Doctrine 2 ORM creates classes with hateful CamelCase


I created yaml configuration for Doctrine. When I'm trying doctrine orm:generate-entities, it creates php files with getters and setters in camel case. So, is_public field transforms into setIsPublic and getIsPublic methods. It's owful. How can I get set_is_public and get_is_public? I can manually edit generated php files, but I don't know what will happen when I change the schema.


Solution

  • You can choose a naming strategy that Doctrine will use to generate the items using:

    Using a naming strategy you can provide rules for automatically generating database identifiers, columns and tables names when the table/column name is not given. This feature helps reduce the verbosity of the mapping document, eliminating repetitive noise (eg: TABLE_).

    For your specific case, I think you're looking at something like:

    $namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_LOWER);
    $configuration()->setNamingStrategy($namingStrategy);
    

    The linked topic goes on to show you how you can write your own custom naming strategy.

    If you're using Symfony, it's even easier (like most things are with Symfony, but that's just my opinion) via config.yml:

    doctrine:
        orm:
            naming_strategy: doctrine.orm.naming_strategy.underscore