标签(空格分隔): Yii2
本文使用Yii2 高级模板,参考 http://www.jianshu.com/p/a3b4b79a2f3f
1 生成 ActiveRecord
1.1 创建 table
登陆数据库,运行以下sql
create table if not exists `news`(
`id` int unsigned auto_increment,
`title` varchar(50) not null,
`content` text,
`created_at` timestamp,
`updated_at` timestamp,
primary key (`id`)
)engine=innodb default charset=utf8
1.2 生成 NewsModel
在浏览器地址栏输入
http://admin.baojia.local/index.php?r=gii

选择 “Model Generator” (点击 Gii 首页的链接)去生成 Model:
Table Name: news
Model Class: News
NameSpace:common\models

然后点击 “Preview” 按钮。你会看到 common/models/News.php 被列在将要生成的文件列表中。可以点击文件名预览内容。
如果你已经创建过同样的文件, 使用 Gii 可以覆盖它, 点击文件名旁边的 diff 能查看现有文件与将要生成的文件的内容区别。

想要覆盖已存在文件,选中 “overwrite” 下的复选框然后点击 “Generator”。如果是新文件,只点击 “Generator” 就好。
接下来你会看到一个包含已生成文件的说明页面。如果生成过程中覆写过文件, 还会有一条信息说明代码是重新生成覆盖的。
2 生成 CRUD 代码
CRUD 代表增,查,改,删操作,这是绝大多数 Web 站点常用的数据处理方式。选择 Gii 中的 “CRUD Generator” (点击 Gii 首页的链接)去创建 CRUD 功能。本例 “news” 中需要这样填写表单:
Model Class: common\models\News
Search Model Class: common\models\NewsSearch
Controller Class: backend\controllers\NewsController

然后点击 “Preview” 按钮。你会看到下述将要生成的文件列表。

如果你之前创建过 backend\controllers\NewsController 和 backend/views/news/index.php 文件, 选中 “overwrite” 下的复选框覆写它们(之前的文件没能全部支持 CRUD)。
运行
在浏览器中输入以下URL
http://admin.baojia.local/index.php?r=news%2Findex
查看/删除

新增

编辑

网友评论