easyswoole超级简易增删改查入门

框架安装

直接按官网操作
https://www.easyswoole.com/Cn/QuickStart/install.html

数据准备

CREATE TABLE `cases`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `title` varchar(200) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '标题',
  `content` text CHARACTER SET utf8 COLLATE utf8_general_ci NULL COMMENT '内容',
  `create_time` int(10) NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = MyISAM AUTO_INCREMENT = 21 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

随便写入几条数据

INSERT INTO `test`.`cases`(`id`, `title`, `content`, `create_time`) VALUES (1, '示剑网络副总经理接受电视台采访', 'test', 1536223828);
INSERT INTO `test`.`cases`(`id`, `title`, `content`, `create_time`) VALUES (3, '微信小程序超级入口,新一轮红利又将爆发', 'test', 1605757832);
INSERT INTO `test`.`cases`(`id`, `title`, `content`, `create_time`) VALUES (4, '北京小程序开发公司哪家好?', 'test', 1535963051);

配置mysql数据库连接

打开easyswoole根目录下的dev文件,跟MAIN_SERVER同级,新增

'MYSQL'=>[
     'host' => '你自己的IP',
     'port' => '端口',
     'user' => 'root',
     'password' => '123456',
     'database' => 'test',
     'timeout' => 5,
     'charset' => 'utf8mb4',
],

修改成你自己的配置

建立模型

在easyswoole的App/Models对应目录下建立模型Case.php

写入代码

控制器部分

/App/HttpController目录下,建立Index.php,写入内容

接下来可以增删改查了


增删改查之“增”

控制器Index里添加代码

public function insert()
{
     $case = new Cases();
     $case->title = 'aaa';
     $case->content = 'test';
     $case->create_time = time();
     $id = $case->save();
     $this->writeJson(0, [], $id > 0 ? '新增成功' : '新增失败');
}

浏览器输入
http://你自己的IP:9501/insert

查看数据库,数据添加成功

增删改查之“查”

控制器Index里添加代码

 public function getAll()
 { 
     $request = $this->request();
     $keyword = $request->getRequestParam('keyword');
     $page = $request->getRequestParam('page');
     $case = new Cases();
     $result = $case->getAll($page, $keyword);
     $this->writeJson(0, $result, "success");
 }

模型/App/Cases.php添加代码

public function getAll(int $page = 1, string $keyword = null, int $pageSize = 3): array
 {
     $list = $this->limit($pageSize * ($page - 1), $pageSize)->order($this->primaryKey, 'DESC')->withTotalCount()
     ->all(function (QueryBuilder $queryBuilder) use ($keyword) { //用use才能将外部参数传入闭包
     $queryBuilder->where('title', "%{$keyword}%", 'like');
     });
     $total = $this->lastQueryResult()->getTotalCount(); //获取结果条数
     return ['total' => $total, 'list' => $list];
 }

到你装easywoole下的Linux命令行,Ctrl+C,再输入php easyswoole start重启服务
回到windows,浏览器输入
http://你自己的IP:9501/getAll?keyword=北京&page=1

成功看到返回信息

{"code":0,"result":{"total":10,"list":[{"id":20,"title":"ddd","content":"test","create_time":1605764790},{"id":19,"title":"ccc","content":"test","create_time":1605764790},{"id":18,"title":"ccc","content":"test","create_time":1605764774}]},"msg":"success"}

增删改查之“删”

Index.php写入代码

public function delete()
{
     $request = $this->request();
     $id = $request->getRequestParam('id');
     $case = Cases::create()->get($id);
     $count = $case->destroy();
     $this->writeJson(0, [], $count > 0 ? '删除成功' : '删除失败');
}

重启服务,访问 你的IP/delete?id=你的数据ID
查看数据库,删除成功

增删改查之“改”

Index.php写入代码

public function update()
{
     $request = $this->request();
     $id = $request->getRequestParam('id');
     $case = Cases::create()->get($id);
     $content = $request->getRequestParam('content');
     $case->content = $content;
     $res = $case->update();
     $this->writeJson(0, [], $res ? '修改成功' : '修改失败');
}

重启服务,访问 你的IP/update?id=你的ID&content=你的内容
查看数据库,删除成功

编码查询语句过程中,如果有什么疑问,就翻下文档吧
https://www.easyswoole.com/Cn/Components/Orm/update.html

就这么简单,搞定

你可能感兴趣的:(php,easyswoole,swoole)