Home  >  Article  >  php教程  >  yii学习之三 使用gii实现实现CRUD 操作

yii学习之三 使用gii实现实现CRUD 操作

WBOY
WBOYOriginal
2016-06-06 20:08:351456browse

??? GII是YII的可视化代码生成模块,比使用命令行方便快捷的多. ??? 最新的YII里都自带了GII模块,所以不必再安装了,默认没有启用,只有配置一下就好了. ??? 依然是pretected/config/main.php 文件 ??? modules=array( ??????? // uncomment the following

??? GII是YII的可视化代码生成模块,比使用命令行方便快捷的多.

??? 最新的YII里都自带了GII模块,所以不必再安装了,默认没有启用,只有配置一下就好了.

??? 依然是pretected/config/main.php 文件

??? ‘modules’=>array(
??????? // uncomment the following to enable the Gii tool
??
?? ???? ‘gii’=>array(
?????? ?’class’=>’system.gii.GiiModule’,
?????? ?’password’=>’123456′,
?????? ?// If removed, Gii defaults to localhost only. Edit carefully to taste.
?????? ?’ipFilters’=>array(’127.0.0.1′,’::1′),
???? ),

??? ?把注释去掉就可以使用gii了,password是密码,ipFilters是IP限制,如果去掉就会限制本机。发布的时候,可以把这个IP改成本机的IP确保安全,不过国内多是动态IP,经常更改IP也比较蛋疼,还是直接关闭GII比较好。

???? 接着在数据库里建立一张表来测试,可以直接导入protected/data/schema.mysql.sql,包含一个tb_user的表和测试数据。

?????打开http://<span style="color: #333333;">localhost</span>/index.php?r=gii??? 输入密码,就可以开始生成代码了。

??? 生成 User 模型

???? 登陆后,点击链接 Model Generator。它将显示下面的模型生成页面,

???? Model Generator

Table Name 输入框中,输入 tbl_user。在 Model Class 输入框中,输入 User。然后点击 Preview 按钮。这里将展示将要生成的新文件。现在点击 Generate 按钮。一个名为 User.php 将生成到 protected/models 目录中。如我们稍后描述的, User 模型类允许我们以面向对象的方式来访问数据表 tbl_user

生成 CRUD 代码

在创建模型类之后,我们将生成执行 CRUD??(create, read, update 和 delete) 操作的代码。我们选择 Gii 中的 Crud Generator,如下所示,

之后输入

http://localhost/index.php?r=user  就可以访问了。
当然如果不闲手疼的话也可以敲命令生成
先切换到当前项目的protected目录
cd E:\wwwroot\test\protected
启动命令
yiic shell E:\wwwroot\test\index.php
生成model
model User tb_user
生成crud
crud User
?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn