开发者

Doctrine 2 ORM creates classes with hateful CamelCase

开发者 https://www.devze.com 2023-04-08 15:18 出处:网络
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

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 开发者_如何学Cwhat will happen when I change the schema.


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


Symfony's coding standards encourage Symfony users to use camelCase:

Naming Conventions

Use camelCase, not underscores, for variable, function and method names, arguments


Personal advice - do not generate entities by doctrine orm:generate-entities.
Use plain PHP to create class. Why?
Orm uses reflection on privates to communicate with database. You dont need to generate setters and getters. I recomend You to use design patterns such as factory or constructor to achive Your goal. Decorators also should work fine.

<?php

class MyClass
{
    private $id;
    private $name;

    public function __construct(int $id, string $name)
    {
        $this->id = $id;
        $this->name = $name;
    }
}


$camelCase is not only Symfony's recomendation for code standard. It's based on PSR2. I highly recomend using PSR2, code gets clean and standarized.
Standard ORM naming strategy is $camelCase private var to snake_case column name. If you want to change it otherwise, consider: other naming stategies

0

精彩评论

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

关注公众号