Fieldnames in DB-Tabels and Gii

Hello,

learning Yii2 I encountered the following problem:

I wanted to use Gii to set up all necessary files for handling with my table ‘mytable’.

Using the Model-generator was successful. Using the CRUD-Generator resulted in an error rigth after clicking ‘Preview’:


syntax error, unexpected '-', expecting '('

The Error was found in ‘models\Mytable’ at line 114, which reads


public function getSN-Unit()

The corresponding tablefield in the Database reads ‘SN-Unit’.

The table ‘mytable’ relates to the table ‘subnational_units’ and the field ‘SN-Unit’ corresponds to the field ‘ISO3166-2’ in the table ‘subnational_units’. The above mentioned function that threw the error says




public function getSN-Unit()

{

  return $this->hasOne(SubnationalUnits::className(), ['ISO3166-2' => 'SN-Unit']);

}



which, as far as I understood, tries to set up relational rules for the ActiveRecord. It seems Gii has a problem with table and field-names not follwing a specific naming-convention, specifically if a ‘-’ is used, is that right?

Do I have to rename all fields with ‘-’?

hbergman

Yes.

Not Gii alone but also ActiveRecord will not work with a column name containing ‘-’.

ActiveRecord tries to define each column of the table as a property by reading its table scheme. A database table column may have a name with ‘-’, but a PHP name (label) can not.

Thanx! I really appreciate your help!

hbergman