Yii generate model without Gii

做~自己de王妃 提交于 2019-12-23 03:12:16

问题


I need to generate a model file without the use of Gii. Are there any command Yii?

$table = "myTable";
Yii::app()->generateModel($table); // ?

回答1:


Perhaps is officially deprecated, you can generate code with Yii Command Line Tools

I have tested it with Yii 1.1.17.

First you need to create a new file on protected/commands called for example NewmodelCommand.php to create a new yii command. We need to avoid to use shell interactive tool and call command directly from our code in controllers, models, etc. To get it, we inherit Yii core class ModelCommand. This class originally force person to type on interactive shell.

<?php

Yii::import('system.cli.commands.shell.ModelCommand');


class NewmodelCommand extends ModelCommand
{

}

That is all. You can test command from CLI into your operating system. In Linux, open your terminal and go to /protected/ directory and type:

./yiic

You will see something like this:

...
The following commands are available:
 - message
 - migrate
 - newmodel
 - shell
 - webapp
...

Play a little with it. Type again:

./yiic newmodel

And you will see all command help and documentation.

To generate a model with this command you need at least model_name as first parameter. Command will use same model name as database table name:

./yiic newmodel MyNewModel

If you have different model and database name:

./yiic newmodel MyNewModel tbl_new_model

If you have troubles using yiic, locating/connecting your db, etc, make sure to setup properly your console environment on protected/config/console.php and check all official docs about Yii console applications.

Finally in your code you can use your command as you want:

$path = '/full/path/to/protected';
$new_model_name = 'MyNewModel';
shell_exec( $path . "/./yiic newmodel $new_model_name" );



回答2:


Don't think there is a command. You could create the model, or the command, yourself. Other option would be to make the requests to Gii via curl.



来源:https://stackoverflow.com/questions/41123218/yii-generate-model-without-gii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!