欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > 学习路之PHP--easyswoole操作数据库

学习路之PHP--easyswoole操作数据库

2025/6/6 14:30:17 来源:https://blog.csdn.net/hopetomorrow/article/details/148311934  浏览:    关键词:学习路之PHP--easyswoole操作数据库

学习路之PHP--easyswoole操作数据库

  • 0、安装orm插件
  • 一、创建数据库
  • 二、创建模型
  • 三、控制器显示
  • 四、效果
  • 五、问题

0、安装orm插件

composer require easyswoole/orm

一、创建数据库

在这里插入图片描述

  1. 表:
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;
  1. 随便写入几条数据
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);
  1. 配置mysql数据库连接
    打开easyswoole根目录下的dev文件,跟MAIN_SERVER同级,新增
'MYSQL'=>['host' => '你自己的IP','port' => '端口','user' => 'root','password' => '123456','database' => 'test','timeout' => 5,'charset' => 'utf8mb4',
],
  1. 注册配置的mysql数据库
    EasySwooleEvent.php
use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Db\Connection;class EasySwooleEvent implements Event
{public static function initialize(){// TODO: Implement initialize() method.date_default_timezone_set('Asia/Shanghai');$config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));DbManager::getInstance()->addConnection(new Connection($config));}

二、创建模型

App\Models\Case.php

<?php
namespace AppModels;
use EasySwoole\ORM\AbstractModel;
use EasySwoole\Mysqli\QueryBuilder;
/*** 案例模型*/
class Cases extends AbstractModel
{/*** @var string*/ protected $tableName = 'cases'; //表名protected $primaryKey = 'id'; //主键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];}}

三、控制器显示

App\HttpController\Index.php

use EasySwoole\Http\AbstractInterface\Controller;
use App\Models\Cases;class Index extends Controller
{public function index(){$request = $this->request();$keyword = $request->getRequestParam('keyword');$page = $request->getRequestParam('page')??1;$case = new Cases();$result = $case->getAll($page, $keyword);$this->writeJson(0, $result, "success");

四、效果

在这里插入图片描述

五、问题

  1. ERROR php_swoole_server_rshutdown() (ERRNO 503): Fatal error: Class ‘EasySwoole\ORM\AbstractModel’ not found in /www/wwwroot/easyswoole/App/Models/Cases.php on line 11

解决方案是重启进程
在这里插入图片描述
2. connection : default not register

connection : default not register
#0 /www/wwwroot/easyswoole/vendor/easyswoole/orm/src/Utility/PreProcess.php(18): EasySwoole\ORM\AbstractModel->schemaInfo()
#1 /www/wwwroot/easyswoole/vendor/easyswoole/orm/src/AbstractModel.php(553): EasySwoole\ORM\Utility\PreProcess::mappingWhere(Object(EasySwoole\Mysqli\QueryBuilder), Object(Closure), Object(App\Models\Cases))
#2 /www/wwwroot/easyswoole/App/Models/Cases.php(25): EasySwoole\ORM\AbstractModel->all(Object(Closure))
#3 /www/wwwroot/easyswoole/App/HttpController/Index.php(19): App\Models\Cases->getAll(1, NULL)

解决方案。注册mysql数据库配置,
EasySwooleEvent.php


use EasySwoole\ORM\DbManager;
use EasySwoole\ORM\Db\Connection;$config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL'));
DbManager::getInstance()->addConnection(new Connection($config));

最后重启进程
在这里插入图片描述

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词