完整示例

演示说明

以下代码演示了基于一个具体数据表的完整模型,实现了数据表的增、删、改、查、缓存、业务逻辑等功能的封装;

数据表结构

/* 模拟一个站内消息通知系统的数据表 */
CREATE TABLE `grace_notifications` (
  `notification_id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `sender_uid` int(11) NOT NULL COMMENT '发送者ID',
  `recipient_uid` int(11) NOT NULL COMMENT '接收者ID',
  `action_type` tinyint(4) DEFAULT NULL COMMENT '通知类型',
  `model_type` smallint(6) DEFAULT NULL COMMENT '模块类型',
  `source_id` int(11) DEFAULT NULL COMMENT '关联id',
  `add_time` int(11) DEFAULT NULL COMMENT '添加时间',
  `read_status` tinyint(4) DEFAULT NULL COMMENT '查看状态',
  `content` text COMMENT '消息内容',
  PRIMARY KEY (`notification_id`),
  KEY `sender_uid` (`sender_uid`),
  KEY `recipient_uid` (`recipient_uid`),
  KEY `action_type` (`action_type`),
  KEY `model_type` (`model_type`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;

模型代码示例

以下代码仅提供参考和一些封装思路,开发时请根据具体业务情况进行更详细的封装。

<?php

namespace phpGrace\models;
class notifications extends \graceModel{
   
   // 构造函数 初始化模型
   public function __construct($connectDB = true){
      $this->tableName  = 'notifications';
        $this->tableKey   = 'notification_id';
      parent::__construct($connectDB);
   }
   
   // 校验数据方法
   public function checker(){
      $checkRules   = array(
         'sender_uid'  => array('int', '1,11', '发送者ID应为有效整数'),
         'recipient_uid'  => array('int', '1,11', '接收者ID应为有效整数'),
         'action_type'  => array('int', '1,4', '通知类型应为1-4位整数'),
         'model_type'  => array('int', '1,4', '模块类型应为1-4位整数'),
         'source_id'  => array('int', '1,11', '关联id应为有效整数'),
         'content'  => array('string', '1,', '消息内容不能为空')
      );
      $checker      = new \phpGrace\tools\dataChecker($_POST, $checkRules);
      $checkRes     = $checker->check();
        if(!$checkRes){\result(false, $checker->error);}
   }
   
   // 添加数据方法
   public function add(){
      if(isset($_POST['file'])){unset($_POST['file']);}
        $this->checker();
      $_POST['add_time']    = time();
        $_POST['read_status'] = 1;
      $res = $this->db->add();
      if($res){return $res;}
        \result(false, $this->error());
   }
   
   // 删除数据方法
   public function delete($gets){
      if(empty($gets[0])){\result(false,'数据格式错误');}
      $res = $this->db->where($this->tableKey.' = ?', array($gets[0]))->delete();
        \result();
   }
   
   // 编辑数据方法
   public function edit($gets){
      if(empty($gets[0])){\result(false,'数据格式错误');}
      if(isset($_POST['file'])){unset($_POST['file']);}
        $this->checker();
      $res = $this->db->where($this->tableKey.' = ?', array($gets[0]))->update();
      if($res){\result();}
        \result(false, $this->error());
   }

   // 查询数据 缓存形式
    public function getDataList($everyPageNumber = 15){
        // 模型的 cache 函数会返回经过缓存逻辑处理后的最终数据结果
        $this->everyPageNumber = $everyPageNumber;
        return $this->cache(
            'notificationsList',
            PG_PAGE.$everyPageNumber, // 每一页数据一个缓存 + 每页展示数
            '__getDataList'
        );
    }

   // 查询数据 动态
   public function __getDataList(){
      return $this->db
            ->order($this->tableKey.' desc')
            ->page($this->everyPageNumber)
            ->fetchAll();
   }

    // 删除相关缓存 [ 只删除 notifications 相关的缓存 ]
    public function removeCaches($id = 0){
        $this->getCacher();
        if($id > 0){
            $this->cacher->removeCacheLikeName(
                array('notificationsList', 'notificationsInfo'.$id)
            );
        }else{
            $this->cacher->removeCacheLikeName(array('notificationsList'));
        }
    }

    // 其他自定义方法
    public function otherFun(){
       echo 'other function ....';
    }
   
}

相关说明

有了上面的模型类,我们就可以方便地在控制器、视图、其他模型内调用模型  ( new \phpGrace\models\模型类(); ) 并使用内部封装的方法。