Active Record Automatic Model Generation


is it possible to automatically generate the model files for a given DB Schema?

This would be great :)

My second question:

What is the best was to get autocompletion on Models(properties) with my IDE(phpStorm)?

Thanks for your help,


Yes. The tool is called Gii. Here’s the guide:

When it generates models it add metadata to models that helps you to get autocomplete in IDEs.

Thanks. Great tool, but it has two big drawbacks :(

1.) It is not possible to build the models files for the whole db at once. Doinig it for each table is not so great(with 100+ tables)

2.) As we are developing our database the generator will overwrite out changes to the model files when we rebuild the model files after changes to the db :(

In my opinion the generator should build two files: a base files and a file where we can implement our logic…

Like this:(For a user table):

  • UserBase.php (Here we only have the generator logic, so this file can be created on each model build run).

  • User.php (extents UserBase.php) (Here the developer can implements it’s logic and it won’t be affected by a new model files build run).

Perhaps you can think about this two points :)



  1. Try *.

  2. You can generate models prefixed with "Base" and then inherit from these. It has drawbacks though.

I found this extension:


Will it work with yii2?

No. It is for 1.1.

Where can I find some Documentation to build my own GII Extension?

Great, thank for the Link…

I build my own generator, that does exactly what I need:

1.) Split the Classes in two files: (one generated base class and one implementation file, that is only created, but never changed)

2.) Gives me an option to singularize my class names, so I automatically get the Classname "User" from my table "users"…

3.) change the getter function name for relations: if it is hasOne, the name is singular and if it is hasMany it is plural like this: hasOne: getUser()… hasMany: getUsers()

This works great for generating classes for the complete DB in one run :)