Home > php教程 > PHP开发 > body text

[ThinkPHP Series] CURD operation of Thinkphp framework (3)

黄舟
Release: 2016-12-28 10:38:36
Original
1199 people have browsed it

a) What is CURD operation?
C (create): create; U (update): update; R (read): read; D (delete): delete

NewsController.class.php
<?php
namespace Home\Controller;
use Think\Controller;
class NewsController extends Controller{
    //添加操作
    public function add(){
        //实例化news模型,news为数据库中的news表 
        $newsModel=M("news"); 
        //create() :根据表单提交的POST数据创建数据对象
        $data = $newsModel->create();
        //add() :写入数据到news数据库中
        if($newsModel->add($data)){
        /**
         * success() 和error()的有三个参数
         * 第一个参数表示提示信息
         * 第二个参数表示跳转地址
         * 第三个参数是跳转时间(单位为秒)
         * */
            $this->success(&#39;添加成功&#39;,&#39;all&#39;,5);
        }else{
            $this->error(&#39;添加失败&#39;);
        }
    }
    //读取操作(read)
    public function all(){
        $newsModel=M(&#39;news&#39;);
        //$newsModel->select(): select * from news
        $data=$newsModel->select();
        //assign(): 分配数据(相当于赋值操作)
        $this->assign(&#39;news&#39;,$data);
        $this->display();
    }
    /**
    * 更新操作
    * 注意:在对应的静态页中对应的form表单中要添加一句
    * <input type=”hidden”name=”id”value=”{$news.id}”>
    * 其中name的值为news表的主键,value的值为控制器中传过来的值
    * 是数据库中对应的主键。
    **/
    public function edit(){
        if(IS_POST){
            $newsModel=M("news");
            $data=newsModel->create();
            //save() :根据条件更新记录
            if($newsModel->save($data)){
                $this->success(&#39;修改成功&#39;,&#39;all&#39;,3);
            }else{
                $this->error(&#39;修改失败&#39;);
            }
        }else{
    /**
    * I():Thinkphp中简单的获取参数的方式
    * I(“post.id”): $_POST[‘id’];
    * I(“get.id”): $_GET[‘id’];
    * param变量类型是框架特有的支持自动判断当前请求类型的变量获取方式
    **/
        $id = I(&#39;id&#39;); //等同于I( &#39;param.id&#39;)
        $newsModel = M("news");
        //find($id):读取id = $id 的新闻数据
            $data = $newsModel->find($id);
            $this->assign(&#39;news&#39;,$data);
            $this->display();
        }
    }
    //删除操作(delete)
    public function del(){
        $newsModel = M(&#39;news&#39;);
        $id = I(&#39;id&#39;);
        if(isset($id) && $newsModel->delete($id)){
            $this->success(&#39;删除成功&#39;);
        }else{
            $this->error(&#39;删除失败&#39;);
        }
    }
}
?>
Copy after login

Okay, the simple basic CURD operations of ThinkPHP are introduced here. For more detailed usage, please see the official online documentation:

a) Version 3.1 manual: http://doc.thinkphp.cn/manual.html
b) Version 3.2 manual: http ://document.thinkphp.cn/manual_3_2.html

The above is the content of [ThinkPHP series] CURD operation of Thinkphp framework (3). For more related content, please pay attention to the PHP Chinese website (www.php. cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template