Skip to content

Instantly share code, notes, and snippets.

@fillup
Last active August 29, 2015 14:02
Show Gist options
  • Save fillup/5cdba5990c6576bbbfb0 to your computer and use it in GitHub Desktop.
Save fillup/5cdba5990c6576bbbfb0 to your computer and use it in GitHub Desktop.
Proposal for Yii2 Gii Model generation to create base class and extending class

Gii provides a very easy way to generate Model classes from a database schema. However, model classes often need to be modified in order to add functionality. Once the model has been modified, you can no longer use Gii to create new versions of the model class when the database schema changes.

As a better practice one should use Gii to generate a base class, then create an extending class that is modified as needed. This allows Gii to regenerate the Base class as often as needed whenever the table schema changes.

Ideally Gii would provide an option to automatically generate both the Base class file and the Extending class file if it does not already exist. I think it would be great if part of the Gii configuration had an option to provide a "Base Class Suffix" that would be appended to the base class name and file name and then the extending class file would be created and extend it properly.

For example, for a “user” database table, Gii could create a class/file named UserBase. If there is no User class/file, Gii would also create it and have it extend UserBase. Then whenever the “user” table schema changes, Gii can be used to regenerate the UserBase file without losing anything previously added to User such as additional rules and methods.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment