如何使教义生成的列名显示在教义2 symfony中的驼峰式案例中? [英] how to make doctrine generated column name to display in camel case in doctrine 2 symfony?

查看:136
本文介绍了如何使教义生成的列名显示在教义2 symfony中的驼峰式案例中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

在数据库中,列名称为 COUNTRY_ID SQL结果将给出assoc。

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

解决方案

您必须实现命名策略才能获取camelCase自动生成的列名,例如在《 Doctrine》文档中进行了解释



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

 <?php 
类CamelCaseNamingStrategy实现NamingStrategy
{
公共函数classToTableName($ className)
{
返回'cc_'。 substr($ className,strrpos($ className,‘\\’)+ 1);
}
公共函数propertyToColumnName($ propertyName)
{
return $ propertyName;
}
公共函数referenceColumnName()
{
返回 id;
}
公共函数joinColumnName($ propertyName,$ className = null)
{
return strtolower($ propertyName)。 ucwords($ this-> referenceColumnName());
}
公共函数joinTableName($ sourceEntity,$ targetEntity,$ propertyName = null)
{
return strtolower($ this-> classToTableName($ sourceEntity))。 ucwords($ this-> classToTableName($ targetEntity));
}
公共函数joinKeyColumnName($ entityName,$ referencedColumnName = null)
{
return strtolower($ this-> classToTableName($ entityName))。 ($ referencedColumnName?:ucwords($ this-> referenceColumnName()));
}
}

然后将该新类注册为服务,并添加它到您的 config.yml

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


I have an entity generated using doctrine in command line. It is as below -

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

In the database the column name is COUNTRY_ID and the SQL result will give assoc. array with COUNTRY_ID as key and its name as value.

My requirement is to have display name of SQL result to camel case. for example COUNTRY_ID should be countryId. Is there any config ready available in doctrine file to do this?.

解决方案

You have to implement a naming strategy to get camelCase autogenerated column names, as explained in Doctrine documentation.

Make a class to get camelCase name for your column names, CamelCaseNamingStrategy.php:

<?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()));
    }
}

Then register this new class as a service, and add it to your config.yml:

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

这篇关于如何使教义生成的列名显示在教义2 symfony中的驼峰式案例中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆