easyswoole - orm使用

1.通用Model

新建文件夹Model并创建文件BaseModel.php

<?php
namespace App\Model;

class BaseModel extends \EasySwoole\ORM\AbstractModel //继承\EasySwoole\ORM\AbstractModel 关键点
{
    protected $pre = 'qps_';//数据库前缀

    public function __construct(array $data = [])
    {
        $this->tableName = $this->pre . $this->tableName;
        parent::__construct($data);
    }

    public function tableName(?string $name = null, bool $is_temp = false)
    {
        if ($name !== null) $name = $this->pre . $name;
        return parent::tableName($name, $is_temp); // TODO: Change the autogenerated stub
    }
}

2.新建Model层

新建文件DeviceModel.php

<?php

namespace App\Model;

class DeviceModel extends \App\Model\BaseModel //继承baseModel
{
    protected $tableName = 'device'; //数据表名


    /** 
     * 分页列表
     * @getAll
     * @param  int  $page  1
     * @param  int  $pageSize  10
     * @param  string  $field  *
     * @return array[total,list]
     */
    public function getAll(int $page = 1, int $pageSize = 10, string $field = '*', array $condition = [], array $order = []): array
    {
        $list = $this
            ->withTotalCount()  //需使用此函数才可获取数据总数量(即下面total)
            ->order($this->schemaInfo()->getPkFiledName(), 'DESC') //主键倒序
            ->where($condition)
            ->field($field)
            ->limit($pageSize * ($page - 1), $pageSize) 
            ->all();
        $total = $this->lastQueryResult()->getTotalCount();
        return ['total' => $total, 'list' => $list];
    }
    /**
     * 获取数据列表
     */
    public function getList(string $field = '*', array $condition = [], array $order = []): object
    {
        $list = $this
            ->withTotalCount()
            ->order($order)
            ->where($condition)
            ->field($field)
            ->all();
        return $list;
    }
}

3.控制器中使用

function ormTest()
    {
        try {
            DbManager::getInstance()->startTransaction();

            $param = $this->request()->getRequestParam();

            //列表
            $deviceList = DeviceModel::create()->getAll($param['page'], $param['limit'], '*', ['status' => '0']);
            $this->writeJson(200, $deviceList);

            // 获取单条数据
            $deviceInfo = DeviceModel::create()->get(['id' => $param['id']]);
            $this->writeJson(200, $deviceInfo);


            // 添加
            $insertRes = DeviceModel::create([
                'name' => 'device501',
                'id'   => '501',
                'status' =>  0
            ], false)->save();
            $this->writeJson(200, $insertRes);


            // 更新
            $updateRes = DeviceModel::create()->update([
                'name' => 'device501asdas',
                'status' =>  0
            ], ['id' => $param['id']]);
            $this->writeJson(200, $updateRes);

            //删除
            $delRes = DeviceModel::create()->destroy($param['id']);
            $this->writeJson(200, $delRes);

            DbManager::getInstance()->commit();
        } catch (\Throwable  $e) {
            DbManager::getInstance()->rollback();
            $this->writeJson(404, $e->getMessage());
        }
    }
posted @ 2022-10-09 11:59  Myifb  阅读(100)  评论(0编辑  收藏  举报