首页 > 其他分享 >easyswoole - orm使用

easyswoole - orm使用

时间:2022-10-09 12:13:46浏览次数:34  
标签:name DeviceModel writeJson easyswoole create param orm 使用 id

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());
        }
    }

标签:name,DeviceModel,writeJson,easyswoole,create,param,orm,使用,id
From: https://www.cnblogs.com/myifb/p/16771575.html

相关文章

  • 枚举类的使用
    1、为什么使用枚举在Java中,我们可以通过​​staticfinal​​​来定义常量。例如,我们希望定义周一到周日这7个常量,可以用7个不同的​​int​​表示:publicclassWeekday{......
  • useImperativeHandle的使用方法
    最陌生的hooks:useImperativeHandle-技术圈(proginn.com) ref的使用普通的类组件有实例所以可以用过React.createRef()挂载到节点或组件上,然后通过this获取到该......
  • Java中如何使用Scanner类读取.txt文件呢?
    转自:http://www.java265.com/JavaJingYan/202111/16359892761682.html 文件是日常开发中,使用最多的一种东西,在操作系统中,万物都以文件的形式存在,如:磁盘,网络,内存都都以......
  • Ceph使用---CephX认证机制及用户管理
    一、CephX认证机制介绍Ceph使用cephx协议对客户端进行身份认证cephx用于对ceph保存的数据进行认证访问和授权,用于对访问ceph的请求进行认证和授权检测,与mon通信的请求都要......
  • easyswoole - mysql以及redis连接池
    1.安装组件安装easyswoole/pool通用连接池组件composerrequireeasyswoole/pool安装easyswoole/redis-pool协程连接池组件composerrequireeasyswoole/redis-p......
  • 使用sharding 做分库分表以后,插入报错 Executing an update/delete query
    这个问题倘若没有 sharding,那就是在service层缺少了事务注解@Transaction这个问题具体看这里​ 我是跑测试类跑出来的问题,好像做分库分表,不能用测试类来测,只能通过 con......
  • arthas使用介绍
    arthas使用介绍一、介绍Arthas是阿里巴巴旗下的一款开源的JAVA诊断工具,可以进行线上监控诊断产品,实时查看应用的内存、gc、线程等信息,方便排查线上问题。二、支持的平......
  • C# DevExpress中GridControl控件的基本属性设置和使用方法
    1.GridControl隐藏GroupPanel(主面板)隐藏:鼠标单击RunDesigner-OptionsView-ShowGroupPanel=False;修改:鼠标单击RunDesigner-Appearance-GroupPanelText="检索信息";......
  • 迅为i.MX8MM开发板使用命令控制GPIO
    在文件系统的/sys目录下给用户提供了许多接口,比如在/sys下面的bus目录,这个目录下放置的都是和总线相关的,比如I2C,usb等。如下图所示:​​​​本章我们需要重点关注下cl......
  • 全志V853+XR829 Tina无线网络wifimanger2.0使用指南
    1.主题Tina无线网络wifimanger2.0使用指南2.问题背景产品:所有需要用到无线的产品硬件:V853+XR829软件:Tina4.0+Wi-Fi配置文件下载:https://bbs.aw-ol.com/topic/1867......