Doctrine 2使用表列作为数据库,而不是camelCase

时间:2018-04-18 12:25:59

标签: php mysql doctrine-orm

你好吗?

所以,最近我决定从Doctrine 1.2迁移到Doctrine 2.5,现在我更新了所有查询和所需的一切。

在Doctrine 1.2中,我在PHP脚本中直接从数据库中生成了所有模型,我决定对Doctrine 2.5做同样的事情。我使用此脚本(https://gist.github.com/tawfekov/4079388)来生成实体。

问题是,它正在生成camelCase中的所有列名。我需要它们的方式与它们在数据库中的方式相同。

这是数据库的样子: Database

以下是实体的样子(反正的一部分): Entity

有谁知道如何解决这个问题?我需要该脚本生成的列名与数据库匹配,而不是在camelCase中。

[编辑] 这不是其他两个问题的重复。第一个问题看起来很有希望,但接受的答案对我来说并不适用。我尝试了几种命名策略,但没有一种能为我工作。第二个问题与我的完全相反。

2 个答案:

答案 0 :(得分:0)

您可以将脚本中的字段名称转换回snake_case

function convertFieldName($input) {
    preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0- 
    9]+)!', $input, $matches);
    $ret = $matches[0];
    foreach ($ret as &$match) {
        $match = $match == strtoupper($match) ? strtolower($match) : 
        lcfirst($match);
    }
    return implode('_', $ret);
 }

答案 1 :(得分:0)

我最终使用@Abdou Rayes建议的想法,即使我必须适应它。

在我的具体情况中,我在数据库中有一些列是camelCase,其他列是snake_case。我必须完全像在数据库中那样维护实体的列名。

因此,在使用this script生成所有实体之后,我决定遍历生成的所有文件并查找我需要替换的所有信息。正好在每个“私人$ nameOfTheVariable”之上;在数据库中有实际列名称的注释。使用正则表达式,我获得了所有实际的列名,以及声明的变量,然后用实际的列名替换变量。

代码如下所示:

    // Changes the variables in the Entity
    $regex = '/ORM\\\\Column\(name="([^"]*)"|private \$([^;]*);/';
    $dir = new DirectoryIterator(__DIR__. '/entities');
    foreach ($dir as $fileinfo) {
        if (!$fileinfo->isDot()) {
            $str = file_get_contents($fileinfo->getRealPath());

            preg_match_all($regex, $str, $matches, PREG_SET_ORDER, 0);

            $variables = array();
            $databaseColumns = array();
            $both = array(&$databaseColumns, &$variables);
            array_walk($matches, function($v, $k) use ($both) { $both[$k % 2][] = $v; }); // to split the column names and the variable names (since the regex returns them in order)
            if( count($databaseColumns) != count($variables) ){ // just in case there are more items in one of the arrays than the other, something must have gone wrong
                echo "Error in file " . $fileinfo->getFilename();
                die;
            }

            $len = count($databaseColumns);
            for($i = 0; $i < $len; $i++){
                $actualColumnName = end($databaseColumns[$i]);
                $nameOfVariableInTheEntity = end($variables[$i]);
                $nameOfVariableInTheEntity = explode(" ", $nameOfVariableInTheEntity); // to remove possible extra stuff after the name of the variable, such as variables with default values in the database
                $nameOfVariableInTheEntity = $nameOfVariableInTheEntity[0];

                $str = str_replace('private $'.$nameOfVariableInTheEntity, 'private $'.$actualColumnName,$str); // replace the declaration of variable
                $str = str_replace('$this->'.$nameOfVariableInTheEntity, '$this->'.$actualColumnName,$str); // replace the usage of the old variables
            }
            file_put_contents(__DIR__.'/entities/'.$fileinfo->getFilename(),$str);
        }
    }