我有一个在命令行中使用主义生成的实体。如下-

/**
 * @var string
 *
 * @ORM\Column(name="COUNTRY_ID", type="string", length=2)
 */
private $cOUNTRYID;


在数据库中,列名称为COUNTRY_ID,SQL结果将为asoc。以COUNTRY_ID为键,名称为值的数组。

我的要求是将SQL结果的显示名称设置为驼峰式。例如,COUNTRY_ID应该为countryId。教义文件中是否有可用的配置可用于执行此操作?

最佳答案

您必须实施一种命名策略来获取camelCase自动生成的列名,如explained in Doctrine documentation

创建一个类以获取您的列名CamelCaseNamingStrategy.php的camelCase名称:

<?php
class CamelCaseNamingStrategy implements NamingStrategy
{
    public function classToTableName($className)
    {
        return 'cc_' . substr($className, strrpos($className, '\\') + 1);
    }
    public function propertyToColumnName($propertyName)
    {
        return $propertyName;
    }
    public function referenceColumnName()
    {
        return 'id';
    }
    public function joinColumnName($propertyName, $className = null)
    {
        return strtolower($propertyName) . ucwords($this->referenceColumnName());
    }
    public function joinTableName($sourceEntity, $targetEntity, $propertyName = null)
    {
        return strtolower($this->classToTableName($sourceEntity)) . ucwords($this->classToTableName($targetEntity));
    }
    public function joinKeyColumnName($entityName, $referencedColumnName = null)
    {
        return strtolower($this->classToTableName($entityName)) . ($referencedColumnName ?: ucwords($this->referenceColumnName()));
    }
}


然后将此新类注册为服务,并将其添加到您的config.yml中:

orm:
    #...
    entity_managers:
        default
            naming_strategy: my_bundle.camel_case_naming_strategy.default

关于symfony - 如何使教义生成的列名显示在教义2 symfony中的驼峰式案例中?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40904550/

10-16 16:58