propelorm / Propel2

Propel2 is an open-source high-performance Object-Relational Mapping (ORM) for modern PHP
http://propelorm.org/
MIT License
1.26k stars 399 forks source link

Columns with hyphens in their names generates invalid constants names #1786

Open clemlatz opened 3 years ago

clemlatz commented 3 years ago

I have a column in a legacy database schema that has hyphens in its name (cart_as-a-gift). The column in my schema.xml (generated using propel:reverse) looks like this:

<column name="cart_as-a-gift" phpName="As-a-gift" type="VARCHAR" size="16"/>

Running model:build works fine but when I'm trying to use the generated classes, I get an syntax error:

Parse error: syntax error, unexpected '-', expecting '=' in /Users/clement/dev/biblys/biblys/src/Model/Map/CartTableMap.php on line 131

The generated table map looks like this:

    /**
     * the column name for the cart_as-a-gift field
     */
    const COL_CART_AS-A-GIFT = 'carts.cart_as-a-gift';

The problem here is that the php constant name stills contains dashes, which is invalid.

A quick workaround I've found for this is to define a tableMapName for the column:

<column name="cart_as-a-gift" phpName="As-a-gift" tableMapName="as_a_gift" type="VARCHAR" size="16"/>

But, as hyphens are valid in mysql columns name, shouldn't propel convert them to underscore when generating the constant name ?

Note : this problem is happening as I'm trying to update from 2.0.0-alpha12 to 2.0.0-beta.1. It didn't happen before. Using 2.0.0-alpha12, the generated constant name is still invalid, but somehow it is never called.

DarkAxi0m commented 2 years ago

Just went to report the same issue, with the same workaround

<column name="[To Name]" tableMapName="ToName" phpName="ToName" type="VARCHAR" size="100"/>

However, there was still some parts of class's that where generated with the column name , not the tableMapName or phpName

We had to change the vendor/propel/propel/src/Propel/Generator/Model/Column.php function getLowercasedName() like this

public function getLowercasedName(): string
    {
       return strtolower($this->getPhpName()); 
       // return strtolower($this->name);
    }