最近这段时间学习thinkphp,感觉确实是很好用的。不过在学习过程中也遇到了一些问题,现在写出来和大家一起分享;
实验目标:表的CURD。
目标及条件:
1. 主键用guid方式,也就是webid能自动以guid进行填充;
2. 插入时能自动填写一些默认值,比如:新增时,字段: isEnabled=1, dataStatus=1,更新时根据实际值改变。
3. 插入时crtDate,updDate自动插入当前时间。
4. 更新时updDate会自动更新当前时间,crtDate不变。
5. 插入和更新时pinyin字段能根据webname字段的值自动填充。
环境:php 5.2.17; think3.12; mysql 5.0.1b
1.创建表
-
drop table if exists ebook_webinfo;
-
-
/*==============================================================*/
-
/* Table: ebook_webinfo */
-
/*==============================================================*/
-
create table ebook_webinfo
-
(
-
webId varchar(64) not null comment \'guid\',
-
webAddr varchar(100) comment \'范例:www.webset.com\',
-
webName varchar(100) comment \'网站名称\',
-
pinYin varchar(100) comment \'拼音码\',
-
crtDate datetime comment \'创建时间\',
-
updDate datetime comment \'最后修改时间\',
-
isEnabled int comment \'启用\',
-
dataStatus int comment \'数据状态\',
-
primary key (webId)
-
);
2. CURD:
创建如下php文件,记得要有thinkphp类库哦:)
1.创建Lib->Action->WebinfoAction.class.php, 注意大小写。
2.创建Lib->Model->WebinfoModel.class.php
3.创建Tpl->Webinfo目录,以及下面的add.html, edit.html文件。
2.1 WebinfoAction.class.php
-
class WebinfoAction extends Action{
private $tablename = 'Webinfo';
public function __construct()
{
}
public function index()
{
// 使用视图显示, Tpl/Webinfo/index.html
$this->name = $this->tablename; // 进行模板变量赋值
// 读取数据库
$Data = M($this->tablename); // 实例化Data数据模型
$this->data = $Data->select();
$this->display();
}
public function insert(){
$Form = D($this->tablename);
if($Form->create()) {
$result = $Form->add();
echo $Form->getLastSql();
if($result) {
$this->success('操作成功!');
}else{
$this->error('写入错误!');
}
}else{
$this->error($Form->getError());
}
}
// 该处的id不能变
public function edit($id=0){
$Form = M($this->tablename);
echo $id;
$this->vo = $Form->find($id);
$this->display();
}
public function update(){
$Form = D($this->tablename);
if($Form->create()) {
$result = $Form->save();
if($result) {
$this->success('操作成功!');
}else{
$this->error('写入错误!');
}
}else{
$this->error($Form->getError());
}
}
}
index.html
-
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
-
<html>
-
<head>
-
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
-
<title>hello {$name}</title>
-
</head>
-
<body>
-
<!-- hello {$name}
-->
-
<table style=\"BORDER-COLLAPSE: collapse\" borderColor=#000000 height=40 cellPadding=1 width=250 align=center border=1>
-
<tr><td>网站ID</td><td>网站名称</td><td>网址</td></tr>
-
<volist name=\"data\" id=\"vo\">
-
<tr>
-
<td>{$vo.webId}</td>
-
<td>{$vo.webName}</td>
-
<td>{$vo.webAddr}</td>
-
</tr>
-
</volist>
-
</table>
-
</body>
-
</html>
add.html
-
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
-
<html>
-
<head>
-
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
-
<title>Webinfo add</title>
-
</head>
-
<body>
-
<FORM method=\"post\" action=\"__URL__/insert\">
-
网站名称:<INPUT type=\"text\" name=\"webName\" ><br/>
-
网址:<INPUT type=\"text\" name=\"webAddr\" ><br/>
-
<INPUT type=\"submit\" value=\"提交\">
-
</FORM>
-
</body>
-
</html>
edit.html
-
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
-
<html>
-
<head>
-
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">
-
<title>编辑</title>
-
</head>
-
<body>
-
编辑状态:
-
<FORM method=\"post\" action=\"__URL__/update\">
-
网站ID:<INPUT type=\"text\" name=\"webId\" value=\"{$vo.webId}\"><br/>
-
网站名称:<INPUT type=\"text\" name=\"webName\" value=\"{$vo.webName}\"><br/>
-
网址:<INPUT type=\"text\" name=\"webAddr\" value=\"{$vo.webAddr}\"><br/>
-
<INPUT type=\"hidden\" name=\"webId\" value=\"{$vo.webId}\">
-
<INPUT type=\"submit\" value=\"提交\">
-
</FORM>
-
</body>
2.2 WebinfoModel.class.php
-
<?php
-
class WebinfoModel extends Model {
-
-
protected $fields = array(
-
'webId',
-
'webName',
-
'webAddr',
-
'pinYin',
-
'crtDate',
-
'updDate',
-
'isEnabled',
-
'dataStatus',
-
'_pk'=>'webId' //主键
-
)
-
;
-
-
// 定义自动验证
-
protected $_validate = array(
-
array('webName','require','名称必须'),
-
);
-
-
// 定义自动完成
-
// guid是我的另外一个函数,在thinkphp中没有,需要自己添加
-
protected $_auto = array(
-
array('webId','guid',1,'function'),
-
array('crtDate','date',1,'function',array('Y-m-d H:i:s')),
-
array('updDate','date',self::MODEL_BOTH,'function',array('Y-m-d H:i:s')),
-
array('isEnabled','1',1,'function'),
-
array('dataStatus','1',1,'function'),
-
array('pinYin','getCurPinYin',3, 'callback'),
-
);
-
-
// 获取拼音
-
function getCurPinYin()
-
{
-
return firstPinyin($_POST['webName']); // firstPinyin是自己定义的获得拼音首字母函数
-
}
}
rizi_cfun_str.php
-
/**
-
* 获取Guid
-
* @return guid
-
*/
-
function guid()
-
{
-
$guid = com_create_guid();
-
$str_grid = str_replace('}','', str_replace('{','',str_replace('-', '', $guid)));
-
return $str_grid;
}
通过以上的实验,可以初步学习thinkphp简单的CURD。
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3090/viewspace-1337494/
,如需转载,请注明出处,否则将追究法律责任。