Gii and SQL Server - won't generate models if a column starts with a number

Hi,

I’m working on a project that requires I use a legacy database - I have no control over the database structure at all, and can make no changes to it. I’ve successfully have the database connection working with Microsoft’s SQLSRV driver, and can create most models fine.

There are tables where column names include numbers, and therefore, SQL Server requires you to put square brackets around the column name. The same thing can be done when you can create columns with spaces (why, I’ve no idea), so long as you add square brackets around it.

So the table structure may be as follows:


CREATE TABLE [dbo].[Genres](

    ID [int] IDENTITY(1,1) NOT NULL,

    [50s] [bit] NULL,

    [60s] [bit] NULL,

    [70s] [bit] NULL

)



However, in attempting to use Gii to generate a model for this table, I recieve the following error:




Column names that does not follow PHP variable naming convention: Genres.50s.



Is there any known solution to this? Like I say, I can’t rename the columns.

Thanks in advance.

Yii uses PHP magic methods (http://php.net/manual/en/language.oop5.magic.php) so that there is no need to create all the variables for the fields of the database table

For example if in a table you have the field "name" in the model you can use $model->name without declaring a property "name"…

That is the reason why the table fields have to follow PHP variable naming convention…

So in your case I’m afraid there is no easy solution… you will miss all the automatic functionality… you would need to create manually the model attributes… but even the commands for retrieving and saving them to proper fields…

I know this is coming more than a tad late but I only just came across this yesterday. What you could do is create views from the erring tables and then create your models from those views.

That’s right but beyond that you must take care of the following as per this post.