Yii在没有Gii的情况下生成模型 [英] Yii generate model without Gii
问题描述
我需要在不使用Gii的情况下生成模型文件.有命令Yii吗?
I need to generate a model file without the use of Gii. Are there any command Yii?
$table = "myTable";
Yii::app()->generateModel($table); // ?
推荐答案
Perhaps is officially deprecated, you can generate code with Yii Command Line Tools
我已经用Yii 1.1.17对其进行了测试.
I have tested it with Yii 1.1.17.
首先,您需要在protected/commands
上创建一个新文件,例如NewmodelCommand.php
ModelCommand
.此类最初迫使人们在交互式外壳上键入.
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
{
}
仅此而已.您可以将命令从CLI测试到操作系统中.在Linux中,打开终端并转到/protected/
目录并键入:
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
...
试一下.再次输入:
./yiic newmodel
您将看到所有命令帮助和文档.
And you will see all command help and documentation.
要使用此命令生成模型,至少需要model_name
作为第一个参数.命令将使用与数据库表名称相同的模型名称:
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
如果您在使用yiic,查找/连接数据库等方面遇到问题,请确保在检查有关Yii控制台应用程序的所有官方文档.
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" );
这篇关于Yii在没有Gii的情况下生成模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!