How To Create Model Programmatically

I know we can create/regenerate model using gii.

Wondering Can we call gii programmatically to create or regenerate model? How?

As my system are going to allow user to create table or add/drop column in existing table.

Thank you.

You’d have to give them access to a modified version of gii that would skip the form or have a simplified form. Gii is actually plugable, so you can make your own generator to acomplish it.

Make my own generator?

I am thinking maybe I can make use of generator in gii, post in needed variables and generate the model?

You could load the generator class and call it directly.

I am trying to call it directly. Below is my code.




public $templates = array();


public function actionIndex() {

$model = new ModelCode;

$model->tableName = 'customer';

$model->modelClass = 'Customer';

$model->modelPath = 'application.models';

$model->template = 'default';

$model->prepare();

}



I am getting this error code.

Do you know anything wrong?

My gii is default from yii framework, I did not change anything on that.

Solved, thanks




public $templates = array();

public $templatePath = '/yii/framework/gii/generators/model/templates/default';


public function actionIndex() {

	$model = new ModelCode;

	$model->tableName = 'customer';

	$model->modelClass = 'Customer';

	$model->modelPath = 'application.models';

	$model->template = 'default';

	$this->templates['default'] = $this->templatePath;

	

	$model->prepare();

	$model->save();

}



Hi, i happened to stumble upon this code. Tried using it but then i get an error that ModelCode.php does not exist or to be specific the error is "include(ModelCode.php): failed to open stream: No such file or directory"

Were you able to find a solution for this?

Thanks