ThinkPHP示例:CURD

完整的控制器文件:

  1. class IndexAction extends Action {
  2.     // 查询数据
  3.     public function index() {
  4.         $Form = M("Form");
  5.         $list = $Form->limit(3)->order('id desc')->select();
  6.         $this->list =  $list;
  7.         $this->display();
  8.     }
  9.     // 写入数据
  10.     public function insert() {
  11.         $Form = D("Form");
  12.         if ($vo = $Form->create()) {
  13.             $list = $Form->add();
  14.             if ($list !== false) {
  15.                 $this->success('数据保存成功!',U('Index/index'));
  16.             } else {
  17.                 $this->error('数据写入错误!');
  18.             }
  19.         } else {
  20.             $this->error($Form->getError());
  21.         }
  22.     }
  23.     // 更新数据
  24.     public function update() {
  25.         $Form = D("Form");
  26.         if ($vo = $Form->create()) {
  27.             $list = $Form->save();
  28.             if ($list !== false) {
  29.                 $this->success('数据更新成功!',U('Index/index'));
  30.             } else {
  31.                 $this->error("没有更新任何数据!");
  32.             }
  33.         } else {
  34.             $this->error($Form->getError());
  35.         }
  36.     }
  37.     // 删除数据
  38.     public function delete($id) {
  39.         if (!empty($id)) {
  40.             $Form = M("Form");
  41.             $result = $Form->delete($id);
  42.             if (false !== $result) {
  43.                 $this->success('删除成功!');
  44.             } else {
  45.                 $this->error('删除出错!');
  46.             }
  47.         } else {
  48.             $this->error('ID错误!');
  49.         }
  50.     }
  51.     // 编辑数据
  52.     public function edit($id) {
  53.         if (!empty($id)) {
  54.             $Form = M("Form");
  55.             $vo = $Form->getById($id);
  56.             if ($vo) {
  57.                 $this->vo   =   $vo;
  58.                 $this->display();
  59.             } else {
  60.                 $this->error('数据不存在!');
  61.             }
  62.         } else {
  63.             $this->error('数据不存在!');
  64.         }
  65.     }
  66. }
原文地址:https://www.cnblogs.com/shanmao/p/3227316.html