自己做的时候太尽兴忘了记笔记了.Tmd还要重新做一遍.这里面是有个小坑的.删了重来一遍吧
QQ20190505-1.png
创建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.php

namespace 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中即可使用

QQ20190506-0.JPG
改下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);

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

Last modification:February 18th, 2020 at 10:32 pm