1

how are you doing?

So, recently I decided to migrate from Doctrine 1.2 to Doctrine 2.5 and right now I'm updating all my queries and everything needed.

In Doctrine 1.2 I had all the models generated directly from the database in a PHP script and I decided to do the same thing for Doctrine 2.5. I'm using this script (https://gist.github.com/tawfekov/4079388) to generate the Entities.

The problem is, it is generating all of the column names in camelCase. I need them in the same way that they are in the database.

Here's what the database looks like: Database

And here's what the Entity looks like (part of it, anyways): Entity

Does anyone know how I can fix this? I need the column names generated by that script to match the database, and not in camelCase.

[EDIT] It's not a duplicate of the other two questions. The first question looked promising but the accepted answer didn't work for me. I tried several naming strategies and none of them worked for me. And the second question is the exact opposite of mine.

Phelps
  • 11
  • 3
  • Possible duplicate of [Doctrine 2 ORM creates classes with hateful CamelCase](https://stackoverflow.com/questions/7573694/doctrine-2-orm-creates-classes-with-hateful-camelcase) – aynber Apr 18 '18 at 12:28
  • Other possible dup: https://stackoverflow.com/questions/40904550/how-to-make-doctrine-generated-column-name-to-display-in-camel-case-in-doctrine – aynber Apr 18 '18 at 12:28

2 Answers2

0

You can convert the field names in your script back to 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);
 }
Abdou Rayes
  • 430
  • 4
  • 18
  • I was looking for an answer directly into Doctrine. If none are found I'll give thi a shot... Although I would have to make some changes, since some fields are camelCase in the database and Doctrine changes them to "camelcase". – Phelps Apr 18 '18 at 17:42
0

I ended up using the idea that @Abdou Rayes suggested, even though I had to adapt it.

In my specific case, I had some columns in the database that were camelCase and others were snake_case. I had to maintain the column names of the entities exactly as they were in the database.

So, after generating all the entities with this script I decided to loop over all the files generated and look for all the info I needed to replace. Right above every "private $nameOfTheVariable;" there was a comment with the actual column name in the database. With a regex I got all the actual column names, as well as the variables declared and then replace the variables with the actual column names.

The code looks like this:

    // 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);
        }
    }
Phelps
  • 11
  • 3