我们把针对某一个数据表或者某个具体的具备面向对象特性的模块封装为一个模型( 不仅仅针对数据表 ),例如 : 会员模型,我们可以封装 : 登录( 多平台、多模式 )、注销、会员信息查询、余额操作、积分操作等等功能;
我们还可以在模型内结合缓存工具类( 请阅本手册的缓存部分 )完成数据缓存功能;
一旦模型封装完毕,您就可以在不同站点、分组的控制器、视图、自定义类等各个部分实例化模型并使用;
下面以 students 模型为例演示常见封装场景 :
请阅读本手册上一节《模型机制》。
例如 : 我们读取数据并直接生成一份表格代码直接输出到视图 ( 数据读取 + 表格展示 ) :
模型封装演示代码
<?php namespace grace\model; use grace\model; class students extends model{ // 数据库配置 public $dbConfig = "mainDB"; // 是否使用表前缀 public $useTablePrefix = true; // 数据表名称 public $tableName = "students"; // 数据表主键名称 public $tablePK = "st_id"; // 数据校验规则 [ 添加数据 ] public $ruleForAdd = array( //"字段名称" => array("验证规则名称", "规则", "验证识别对应错误信息") 'st_name' => array('string', '1,100', '请正确填写您的称呼'), 'st_age' => array('intBetween', '10,199', '请正确填写您的年龄') ); // 数据校验规则 [ 更新数据 ] public $ruleForUpdate = array( //"字段名称" => array("验证规则名称", "规则", "验证识别对应错误信息") ); public function __construct(){ parent::__construct(); } public function getStudentListAsTable(){ // 利用 $_GET['page'] 传递页码 $_GET['page'] = empty($_GET['page']) ? 1 : $_GET['page']; // 利用 $_GET['total'] 保存总数, // 第一次查询或者 url 没有保存总数则查询数据总数 if(empty($_GET['total'])){ $_GET['total'] = $this->table->count(); } $data = $this->table ->order('st_id desc') ->page($_GET['page'], $_GET['total'], 10, 10, 'a') ->fetchAll(); // 表格开始及表头 $tableStr = '<table class="demo-table">'; $tableStr .= '<tr>'; $tableStr .= '<td>ID</td>'; $tableStr .= '<td>姓名</td>'; $tableStr .= '<td>年龄</td>'; $tableStr .= '</tr>'; // 表格数据 foreach ($data[0] as $student){ $tableStr .= '<tr>'; $tableStr .= '<td>'.$student['st_id'].'</td>'; $tableStr .= '<td>'.$student['st_name'].'</td>'; $tableStr .= '<td>'.$student['st_age'].'</td>'; $tableStr .= '</tr>'; } $tableStr .= '</table>'; // 返回表格及分页数据 return array($tableStr, $data[1]); } }
视图文件直接调用模型演示代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> <script type="text/javascript" src="http://lib.sinaapp.com/js/jquery/3.1.0/jquery-3.1.0.min.js"></script> <script src="https://unpkg.com/vue@next"></script> <style> li{list-style:none;} .pager-list{margin-top:15px;} .pager-list .pager{display:block; font-size:14px; color:#333333; background:#F5F5F5; padding:0px 10px; height:30px; line-height:30px; float:left; margin-right:5px; border-radius:2px; cursor:pointer; text-decoration:none;} .pager-list .pager:hover{background:#2F4056; text-decoration:none; color:#FFFFFF !important;} .pager-list .pager:visited{color:#333333;} .pager-list .current{background:#5FB878 !important; color:#FFF !important;} .loading{position:fixed; width:100%; height:100%; text-align:center; font-size:15px; line-height:300px; color:#FFFFFF; z-index:99; left:0; top:0; bottom:0; background:rgba(0,0,0,0.8);} table{width:100%;} td{padding:5px;} </style> </head> <body> <div style="padding:100px;" id="app"> <!-- 表格展示数据列表 --> <?php $studentsModel = new \grace\model\students(); $data = $studentsModel->getStudentListAsTable(); echo $data[0]; ?> <div class="pager-list"> <?php echo $data[1]['firstPage'];?> <?php echo $data[1]['prePage'];?> <?php echo $data[1]['listPages'];?> <?php echo $data[1]['nextPage'];?> <?php echo $data[1]['lastPage'];?> </div> </div> </body> </html>