• PhalAPI学习笔记拓展篇 ———ADM模式中NotORM实现简单CURD


    PhalAPI学习笔记拓展篇 ———ADM模式中NotORM实现简单CURD

    前言

    公司业务需要转学PHP,而PHP中一个功能强大且生态链完整的PHP接口框架 PhalAPI
    值得大家去学习,本学习笔记持续更新!
    虽然官方文档写的十分明白,以及CSDNPhalAPI框架内容也少之又少。
    因此,以自我踩坑为基础,提供一个更为精简的学习笔记,本学习笔记将会省略部分安装及简单操作。

    内容

    本期内容我们来学习一下,如何在PhalAPI框架上快速应用 ADM模式实现CURD

    ADM模式

    通俗易懂的话,就是PhalAPI 框架中不采用传统的MVC架构设计模式,采用ADM开发模式!
    ADM全称:Api-Domain-Model 传统MVC全称:Model-View-Controller
    可见抛弃了传统的 View 层,将Controller改为Api为控制器,由此,开发设计思想产生改变

    ADM简单介绍

    还是由Api Domain Model层进行拆分

    • API (控制器)
    • Domain (中心逻辑处理层)
    • Model (数据库模型)

    层级处理,环环相扣的设计思想

    • API控制器只关心Domain层内逻辑处理调用实现而不直接操作Model层!
    • Domain只关心对于一些核心的业务核心代码进行逻辑处理,并且直接操作Model 层!
    • Model只关心对于数据库表中数据进行处理,只管数据库数据返回!

    用简单的话语讲解了关于 ADM的核心思想,那么接下来我们看看如何进行实现把!

    准备工作

    • 一个数据库
    • 拥有一个API层的PHP文件
    • 拥有一个Domain层的PHP文件
    • 拥有一个Model层的PHP文件

    拥有了这些我们就可以开始实现ADM下的CURD操作啦!

    PhalAPI提供的CURD操作方法

    Insert(插入)

    • insert()
    • insert_id()
    • insert_multi()
    • insert_update()

    Update(修改)

    • update()
    • updateCounter()
    • updateMultiCounters()

    Select(查询)

    • queryAll()

    delete(删除)

    • delete()

    业务实现

    Model

    namespace App\Model;
    
    use PhalApi\Exception;
    use PhalApi\Model\NotORMModel as NotORM;
    
    class Operation extends NotORM
    {
        protected function getTableName($id)
        {
            return "operation";
        }
        public function csdn_insert(){
            $array = array("path" => "/csdn" ,"project" => "csdn");
            return $this->getORM()->insert($array);
        }
    
        public function csdn_mult_insert(){
            $array = array(array("path" => "/csdn2" ,"project" => "mult_csdn1"),array("path" => "/csdn3" ,"project" => "mult_csdn2"));
            return $this->getORM()->insert_multi($array);
        }
    
        public function csdn_insert_update(){
            $unique = array("id"=>"242");
            $insert = array("path" => "/csdn2","project" => "mult_csdn1");
            $update = array("path" => "/updateCsdn2");
    
            return $this->getORM()->insert_update($unique,$insert,$update);
        }
    
    
        public function csdn_update(){
            $updateArray = array("path" => "/csdnUpdate","project" => "csdn");
            return $this->getORM()->where("id = ?",242)->update($updateArray);
        }
    
        public function csdn_update_add(){
            return $this->getORM()->where("id = ?",242)->updateCounter("num",1);
        }
    
        public function csdn_update_mult_add(){
            return $this->getORM()->where("id = ?",242)->updateMultiCounters(array("num"=>1));
        }
    
        public function csdn_queryAll(){
            $sql = "select * from pp_operation";
            return $this->getORM()->queryAll($sql);
        }
    
        public function csdn_delete(){
            return $this->getORM()->where("id = ?",242)->delete();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    Domain

    namespace App\Domain\Operation;
    
    use App\Model\Operation;
    
    class OpaerationDomain
    {
        var $model;
    
        public function __construct()
        {
            $this->model = new Operation();
        }
    
        public function csdn_insert(){
    
           return $this->model->csdn_insert();
        }
    
        public function csdn_mult_insert(){
            return $this->model->csdn_mult_insert();
        }
    
        public function csdn_insert_update(){
            return $this->model->csdn_insert_update();
        }
    
    
        public function csdn_update(){
            return $this->model->csdn_update();
        }
    
        public function csdn_update_add(){
            return $this->model->csdn_update_add();
        }
    
        public function csdn_update_mult_add(){
            return $this->model->csdn_update_mult_add();
        }
    
        public function csdn_queryAll(){
            return $this->model->csdn_queryAll();
        }
    
        public function csdn_delete(){
            return $this->model->csdn_delete();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47

    API

    namespace App\Api\Opaeration;
    
    use PhalApi\Api;
    use App\Model\Operation as Table;
    use App\Domain\Operation\Permission\OpaerationPermissionDomain as PermissionDomain;
    class Opaeration extends  Api
    {
        var $domain;
        var $permissionDomain;
    
        public function __construct()
        {
            $this->domain = new Table();
            $this->permissionDomain = new PermissionDomain();
    
        }
        public function csdn_insert(){
    
            return $this->domain->csdn_insert();
        }
    
        public function csdn_mult_insert(){
            return $this->domain->csdn_mult_insert();
        }
    
        public function csdn_insert_update(){
            return $this->domain->csdn_insert_update();
        }
    
    
        public function csdn_update(){
            return $this->domain->csdn_update();
        }
    
        public function csdn_update_add(){
            return $this->domain->csdn_update_add();
        }
    
        public function csdn_update_mult_add(){
            return $this->domain->csdn_update_mult_add();
        }
    
        public function csdn_queryAll(){
            return $this->domain->csdn_queryAll();
        }
    
        public function csdn_delete(){
            return $this->domain->csdn_delete();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    就完成了ADM设计模式,同时使用了基础的NotORM的CURD方法完成该功能!

    结束语

    关于 PhalAPI学习笔记拓展篇 ———ADM模式中NotORM实现简单CURD 就讲到这里,对你有帮助的话!

    • 点赞
    • 收藏

    谢谢你的观看!

  • 相关阅读:
    【MySQL从入门到精通】【高级篇】(五)MySQL的SQL语句执行流程
    【halcon】halcon轮廓总结之select_contours_xld
    Linux应用基础——串口应用编程
    【Matplotlib】plt.plot() X轴横坐标展示完整整数坐标
    jupyter_快速开始
    我的项目day01:创建项目的前端和后端,以及对前后端项目的基本配置
    高级深入--day34
    十五、环境变量和代理跨域及api的定义
    一名高级的Javaer,应该了解的 MYSQL 高级知识点
    MySQL主从复制和读写分离的原理与实战
  • 原文地址:https://blog.csdn.net/qq_33638188/article/details/125525718