easyswoole数据库连接池_EasySwoole使用Tp风格的数据库

EasySwoole使用Tp风格的数据库

羡仙. • 2019 年 05 月 06 日

自己做的时候太尽兴忘了记笔记了.Tmd还要重新做一遍.这里面是有个小坑的.删了重来一遍吧

创建App\Model\Base.php继承EasySwoole\Mysqli\TpORM自定义

增加update时间 修改EasySwoole\Mysqli\TpORM如果使用createTime并且存在updateTimeName就添加修改时间public function update($data = null)

{

// 对象方式的修改,当data里存在主键的时候走Update会验证dbField的所有字段设置

if (isset($this->data[$this->primaryKey]) && isset($data[$this->primaryKey])) {

return parent::update($data);

} else {

if (!empty($data) && is_array($data)) {

if ($this->createTime === true && !empty($this->updateTimeName)) {

$data[$this->updateTimeName] = time();

}

$sqlData = $this->convertData($data);

$res = $this->getDb()->update($this->dbTable, $sqlData);

return $res;

} else {

return false;

}

}

}

修改Model\Base.php中的edit方法protected function edit($data = null)

{

try {

if($this->createTime === true && !empty($this->updateTimeName)){

$data[$this->updateTimeName] = time();

}

return $this->update($data);

} catch (\EasySwoole\Mysqli\Exceptions\ConnectFail $e) {

$this->throwable = $e;

return false;

} catch (\EasySwoole\Mysqli\Exceptions\PrepareQueryFail $e) {

$this->throwable = $e;

return false;

} catch (\Throwable $t) {

$this->throwable = $t;

return false;

}

}

注意一下__construct()方法中引入的数据库配置。$db = PoolManager::getInstance()->getPool(MysqlPool::class)->getObj(Config::getInstance()->getConf('MYSQL.POOL_TIME_OUT'));

修改成Yaconf的配置文件$db = PoolManager::getInstance()->getPool(MysqlPool::class)->getObj(\Yaconf::get('mysql.POOL_TIME_OUT'));

mysql.ini配置如下host = '127.0.0.1'

port = '3306'

user = 'root'

timeout = '5'

charset = 'utf8mb4'

password = 'root'

database = 'video'

POOL_MAX_NUM = '20'

POOL_TIME_OUT = '0.1'

创建App\Model\Video.php`继承App\Model\Model.phpnamespace App\Model;

use App\Model\Model;

class Video extends Model

{

protected $dbTable = 'test';

protected $softDelete = false;

protected $createTime = true;

protected $createTimeName = 'create_time';

protected $updateTimeName = 'update_time';

protected $dbFields

= [

'content' => ['text', 'required'],

'id' => ['int'],

'name' => ['varchar'],

'create_time' => ['int'],

'update_time' => ['int'],

];

}

创建测试数据库create table `test`(

`id` int(10) unsigned NOT NULL AUTO_INCREMENT PRIMARY key,

`name` varchar(100) NOT NULL DEFAULT '',

`content` text NOT NULL,

`create_time` int(10) unsigned NOT NULL DEFAULT '0',

`update_time` int(10) unsigned NOT NULL DEFAULT '0'

) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8;

如果不设置$dbTable默认会用类名.测试报错

Call to a member function getObj() on null

官方文档的命名空间是这样的。。。use App\Pool\MysqlPool;

use App\Pool\MysqlObject;

而在之前给链接池的文档是这样的。。demo中有封装好的mysql连接池以及mysql类,复制demo中的MysqlPool.php和MysqlObject.php并放入App/Utility/Pool中即可使用

改下App\Model\Model.php的引用mysql的命名空间use App\Utility\Pool\MysqlPool;

use App\Utility\Pool\MysqlObject;

测试添加数据.成功!$res = Video::add(['content' => 'hello.how are you?']);

return $this->response()->write($res);

测试查询数据.成功!$res = Video::where(['id' => ['=', 15]])->edit(['name'=>'test']);

return $this->writeJson(0,'ok',$res);

测试修改数据.成功!并且修改出现更新时间!$res = Video::where(['id' => ['>=', 15]])->field(['id','content'])->select();

return $this->writeJson(0,'ok',$res);

测试删除数据.成功!$res = Video::where(['id' => ['=', 15]])->del();

return $this->writeJson(0,'ok',$res);

还要忙于拯救世界.过多的方法自己测试吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值