thinkphpCURD - juedaiyuer/researchNote GitHub Wiki
#ThinkPHP的CURD操作#
##创建数据##
// Application/Home/View/Form/add.html
<FORM method="post" action="__URL__/insert">
标题:<INPUT type="text" name="title"><br/>
内容:<TEXTAREA name="content" rows="5" cols="45"></TEXTAREA><br/>
<INPUT type="submit" value="提交">
</FORM>
// Application/Home/Controller/FormController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class FormController extends Controller{
public function insert()
{
$Form = D('Form');
if ($Form->create()) {
$result = $Form->add();
if ($result) {
$this->success('数据添加成功!');
} else {
$this->error('数据添加错误!');
}
} else {
$this->error($Form->getError());
}
}
}
#访问URL
...index.php/home/Form/add
##测试数据库##
CREATE TABLE IF NOT EXISTS `think_form` (
`id` smallint(4) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`content` varchar(255) NOT NULL,
`create_time` int(11) unsigned NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
##D函数模型类##
// 模型名+Model.class.php
// Application/Home/Model/
namespace Home\Model;
use Think\Model;
class FormModel extends Model {
// 定义自动验证
protected $_validate = array(
array('title','require','标题必须'),
);
// 定义自动完成
protected $_auto = array(
array('create_time','time',1,'function'),
);
}
如果使用D函数实例化模型类,一般需要对应一个数据模型类
##测试##
#前端插入成功后,查看mysql数据库
mysql> select * from think_form;
+----+--------+--------------------+-------------+
| id | title | content | create_time |
+----+--------+--------------------+-------------+
| 1 | 测试 | 这是一个测试 | 1470281800 |
+----+--------+--------------------+-------------+
1 row in set (0.00 sec)
##读取数据##
// 控制层
// Application/Home/Controller/FormController.class.php
public function read($id=0){
$Form = M('Form');
// 读取数据
$data = $Form->find($id);
if($data) {
$this->assign('data',$data);// 模板变量赋值
}else{
$this->error('数据错误');
}
$this->display();
}
// 视图层
// Application/Home/View/Form/read.html
<table>
<tr>
<td>id:</td>
<td>{$data.id}</td>
</tr>
<tr>
<td>标题:</td>
<td>{$data.title}</td>
</tr>
<tr>
<td>内容:</td>
<td>{$data.content}</td>
</tr>
</table>
//访问URL
...index.php/home/Form/read/id/1
##更新操作##
// 视图层
// Application/Home/View/Form/edit.html
<FORM method="post" action="__URL__/update">
标题:<INPUT type="text" name="title" value="{$vo.title}"><br/>
内容:<TEXTAREA name="content" rows="5" cols="45">{$vo.content}</TEXTAREA><br/>
<INPUT type="hidden" name="id" value="{$vo.id}">
<INPUT type="submit" value="提交">
</FORM>
// 控制层
// Application/Home/Controller/FormController.class.php
public function edit($id=0){
$Form = M('Form');
$this->assign('vo',$Form->find($id));
$this->display();
}
public function update(){
$Form = D('Form');
if($Form->create()) {
$result = $Form->save();
if($result) {
$this->success('操作成功!');
}else{
$this->error('写入错误!');
}
}else{
$this->error($Form->getError());
}
}
//访问URL
...index.php/home/Form/edit/id/1
##删除数据##
只需要调用delete方法
##数据库配置##
/* 数据库设置 */
'DB_TYPE' => 'mysql', // 数据库类型
'DB_HOST' => 'localhost', // 服务器地址
'DB_NAME' => 'test', // 数据库名
'DB_USER' => 'root', // 用户名
'DB_PWD' => 'root', // 密码
'DB_PORT' => '', // 端口
'DB_PREFIX' => 'think_', // 数据库表前缀
'DB_PARAMS' => array(), // 数据库连接参数
'DB_DEBUG' => TRUE, // 数据库调试模式 开启后可以记录SQL日志
'DB_FIELDS_CACHE' => true, // 启用字段缓存
'DB_CHARSET' => 'utf8', // 数据库编码默认采用utf8
'DB_DEPLOY_TYPE' => 0, // 数据库部署方式:0 集中式(单一服务器),1 分布式(主从服务器)
'DB_RW_SEPARATE' => false, // 数据库读写是否分离 主从式有效
'DB_MASTER_NUM' => 1, // 读写分离后 主服务器数量
'DB_SLAVE_NO' => '', // 指定从服务器序号
##source##
- ThinkPHP3.2.3快速入门.pdf-快速入门2:CURD