作者:潇湘V烟雨 | 来源:互联网 | 2014-06-12 18:37
CURD增删改查操作,先给出一套代码CURDAction.class.php<?php//测试对于数据库的增删改查操作classCURDActionextendsAction{//index页面publ...
CURD增删改查操作,先给出一套代码CURDAction.class.php
-
-
-
- classCURDActionextendsAction
-
- {
-
-
-
- publicfunctionindex()
-
- {
-
- $this->assign(&#39;title&#39;,&#39;添加数据&#39;);
-
- $this->assign(&#39;datetime&#39;,date("Y-m-dH:i:s"));
-
- $this->display();
-
- }
-
-
-
- publicfunctioncreate()
-
- {
-
-
-
- $demo=D("Demo");
-
- if($demo->create()){
-
-
-
- $demo->autotime=date("Y-m-dH:i:s");
-
-
-
- $demo->add();
-
-
-
-
-
- $this->redirect("read");
-
- }else{
-
- header("Content-Type:text/html;charset=utf-8");
-
- exit($demo->getError().&#39;[返回aaaaaa]&#39;);
-
- }
-
- }
-
-
-
- publicfunctionread()
-
- {
-
- $demo=D("Demo");
-
- $data=$demo->order(&#39;iddesc&#39;)->limit(10)->select();
-
- $this->assign(&#39;data&#39;,$data);
-
- $this->assign(&#39;title&#39;,&#39;添加数据2&#39;);
-
- $this->display();
-
- }
-
-
-
- publicfunctionupdate()
-
- {
-
- $demo=D("Demo");
-
-
-
-
-
-
-
-
-
-
-
- $date[&#39;title&#39;]=$_POST[&#39;title&#39;];
-
- $date[&#39;content&#39;]=$_POST[&#39;content&#39;];
-
- $date[&#39;id&#39;]=$_POST[&#39;id&#39;];
-
- $demo->save($date);
-
- $this->assign(&#39;title&#39;,&#39;更新数据2&#39;);
-
- $this->display();
-
- }
-
-
-
- publicfunctiondelect()
-
- {
-
- $demo=D("Demo");
-
- $demo->where(&#39;id=5&#39;)->delete();
-
- $this->redirect("read");
-
- }
-
- }
-
- ?>
理解一下操作,就能理解上面的代码了,因为要下班了,回家后再作详解.
1。$demo=D("Demo");
通过数据库建立一个指定表的映射对象。
2。$demo->create()
通过相同的表单name属性名,自动的把表单里的值加载到映射对象中
3。$demo->add();
插入操作,将不为空的映射对象里的数据插入到数据库内
4。$date[&#39;title&#39;]=$_POST[&#39;title&#39;];
$date[&#39;content&#39;]=$_POST[&#39;content&#39;];
$date[&#39;id&#39;]=$_POST[&#39;id&#39;];
$demo->save($date);
更新操作,这里做的是无条件更新,系统会自动通过主键更新
5。$demo->where(&#39;id=5&#39;)->delete();
通过设置条件,删除操作。
6。$data=$demo->order(&#39;iddesc&#39;)->limit(10)->select();
$this->assign(&#39;data&#39;,$data);