Automate Gii Generation without using GUI

Hi everyone,

I am attempting to automate the Gii functionality upon app creation in order to build a bespoke CMS that can be used multiple times. So far i have been able to create the script that asks the user for DB connection details and sets up users and user authorisation by using a prebuilt model, what i want to do now to extend this is to automate the Model and CRUD generation for other features chosen by the user whilst going through the setup process. There will be a selection of SQL files that generate only the needed tables for the users bespoke application and from this i wish to generate the Model and CRUD files.

I have been tracking the POST and GET data from manual Gii generation and have followed the process to Framework/gii/generators where there are folders for each of the diffferent options in Gii, e.g. model and crud etc. in each of these folders are the template(s), the views, *code.php and *generator.php. From what i can tell the *code is the controller which actions the generation using the template(s). what i cannot figure out and what im hoping the community can help me with is, from here what do i neeed to pass the variables to in order to automate the Model and CRUD generation without using the Gii GUI.

Hope someone can help as i have been pulling my hair out at this for days.

Kind Regards


hey think ive found something but im struggling to understand what has been suggested. This was posted on Stack Overflow(unable to post link due to restrictions on first posts)


I’m having real problems with the following topic:

I’m creating a social network using Yii framework and for each user I’m generating a bunch of DB tables that start with the user id, so a DB table looks like: 13_my_firends.

The point is that by doing this, I cannot generate a certain model using the gii tool, and I really need some models for this tables to make some relations to avoid inner joins, so I guess the problem is like this:

-> How can I create a model file using php or Yii code and also, within that code alter the "public function relation(){}" method to add my relations?


I was just thinking about this: Is there a possibility to add the relation to the parent and not the son? What I mean is… Can I alter the main DB table (witch in my case is sys_accounts - this one has a model) like: -> call the relations() method in another method -> make the relations array (kind of like a join)

So basically it’s like… altering the relations() method only when I want to. Is it possible? And if so, how?



You can customize gii tool and make it dynamically generate model such as

Yii::app()->getModule(‘ssgii’)->SSGenModel($dynamictableName, $dynamicModelName);

public function SSGenModel($tableName, $modelName){

    $_ccodeGen = $this->controllerMap;

    $ccodeGen = new CCodeGenerator('', null);

    $ccodeGen->codeModel = 'ssgii.generators.model.ModelCode';

    $model=$ccodeGen->ssAutoGenModel($tableName, $modelName);

    return ($model);


my Comments:

but i dont fully understand what is being suggested any help would be nice.


Here is some code I’ve extracted from something I tried several years ago.

Maybe you’ll find some inspiration in it.

class Track extends CActiveRecord


    public $defaultuser_id;//=1;// = 1;  // TODO: remove initialisation

    public $context_id;//=1;// = 1;  // TODO: remove initialisation

    public $raw_id = 0; // By default, no specific reference.


     * Returns the static model of the specified AR class.

     * @return TrackC1L1 the static model class


    public static function model($className=__CLASS__)



        //$m->user_id = $user_id;

        //$m->context_id = $context_id;

        return $m;


    public static function getModel($context_id,$user_id) {

        $track = new Track(null);

        $track->raw_id = 0;



        try {


        } catch (CDbException $e) {


            // TODO: handle errror.

            $sql = "

                    CREATE TABLE IF NOT EXISTS `".$track->tableName()."` (

						// ...

                            `user_id` INT NULL' ,

                            INDEX `idx_id` (`user_id` ASC)



            $dbc= Yii::app()->getDb()->createCommand($sql);

            //Yii::trace($sql); for debug





        return $track;


    public function newInstance($attributes) {

        $model = new Track(null);

        $model->user_id = $this->defaultuser_id;

        $model->context_id = $this->context_id;


        return $model;


    public function setTablename($tn) {

        $this->tableNm = $tn;



     * @return string the associated database table name


    public function tableName()


        return 'track_c'.$this->context_id;//.'_l'.$this->user_id;



     * @return array Default scope for this model.


    public function defaultScope() {

        return array (

                'condition'=>$this->getTableAlias(true, false) .'.`user_id`='.$this->defaultuser_id



    public function beforeValidate() {

        $this->user_id = $this->defaultuser_id;

        return parent::beforeValidate();


    public function modelClassName() {

        return $this->tableName();