Doctrine 2 ORM creates classes with hateful CamelCase

﹥>﹥吖頭↗ 提交于 2019-12-07 12:11:06

问题


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.


回答1:


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



回答2:


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

Naming Conventions

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




回答3:


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



来源:https://stackoverflow.com/questions/7573694/doctrine-2-orm-creates-classes-with-hateful-camelcase

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!