yii2使用gii生成代码
1.查看frontend/web/index.php 是否存在以下代码
这段配置表明,如果当前是开发环境, 应用会包含 gii
模块,模块类是 yii\gii\Module。
2.直接通过url访问http://hostname/index.php?r=gii
3.生成活动记录类
选择 “Model Generator” (点击 Gii 首页的链接)去生成活动记录类。并像这样填写表单:
- Table Name:
country
- Model Class:
Country
- 如果是带表前缀的写全名如:pre_country
4.点击 “Preview” 按钮。你会看到 models/Country.php
被列在将要生成的文件列表中。可以点击文件名预览内容。如果想将model文件生成到common的models下则修改Namespace为common\models,使用表前缀的选上user table prefix.
如果你已经创建过同样的文件, 使用 Gii 会覆写它, 点击文件名旁边的 diff
能查看现有文件与将要生成的文件的内容区别。
想要覆写已存在文件,选中 “overwrite” 下的复选框然后点击 “Generator”。如果是新文件,只点击 “Generator” 就好。
接下来你会看到一个包含已生成文件的说明页面。如果生成过程中覆写过文件, 还会有一条信息说明代码是重新生成覆盖的。
5.生成 CRUD 代码
CRUD 代表增,查,改,删操作,这是绝大多数 Web 站点常用的数据处理方式。选择 Gii 中的 “CRUD Generator” (点击 Gii 首页的链接)去创建 CRUD 功能。本例 “country” 中需要这样填写表单:
- Model Class:
app\models\Country
- Search Model Class:
app\models\CountrySearch
- Controller Class:
app\controllers\CountryController
如果是想将文件生成到backend则这样填 - Model Class:
common\models\Country
- Search Model Class:
common\models\CountrySearch
-
Controller Class:
backend\controllers\CountryController
@backend/views/country
“Preview” 按钮。你会看到下述将要生成的文件列表。
转载于:https://my.oschina.net/u/2544569/blog/1083207