Automatically add @property and @method to Yii CActiveRecord Models

To improve Code Inspection for PHPStorm I needed to automatically add all database attributes as @property to my CActiveRecord classes. Additionally it adds all relations and some common used CActiveRecord methods too.

End Result

A yii commandline utility. You configure it with your db and run it on your codebase. The result looks something like this:

     * @property int id
     * @property string password
     * @property string name
     * @property bool active
     * @property Group group
     * @property array roles
     * @method User|CActiveRecord find()
    class User extends CActiveRecord

It automatically extracts the type from the database.. The bool comes from tinyint(1) fields. Group is a has_one relation and roles is a has_many relation.. Also I add some common used CActiveRecord methods there.


Download it from: github

How the code works

  1. Walk through all files in a "/models/" directory
  2. Extract the tablename from the tableName() function
  3. Fetch the database information with yii - and map the database type into phpdoc types
  4. Extract the relations from the relations() function
  5. Find the class docstring and delete all @property and @method from there
  6. Add both above informations as @property
  7. Add some predefined functions as @method