<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                本實例演示tp5模型非常高效的功能 原來數據表結構如下 ![](https://box.kancloud.cn/2016-08-25_57be99bc07783.png) 存儲數據如下 ![](https://box.kancloud.cn/2016-08-25_57be99bc2108f.png) 如上圖,ip字段,ctime,utime采用了轉換成int進行存儲,方便排序,查找。但直接看表中數據,并不直觀,類以這類表,利用model模型,能夠方便的解決數據自動轉換問題 sys_log表定義model如下 ~~~ <?php namespace app\common\model; use think\Model; use think\Validate; // http://www.hmoore.net/zmwtp/tp5/145712 model配置 class Log extends Model { protected $table = 'sys_log'; protected $auto = [ 'ip','utime']; //自動 完成字段 每次寫入都執行 protected $insert = ['ctime','status'=>0,'type'=>'sys']; //插入數據 protected $update = []; //更新數據時 // 定義時間戳字段名 protected $autoWriteTimestamp = true; //支持默認兩個字段,詳見說明 protected $createTime = 'ctime'; protected $updateTime = 'utime'; // protected $autoTimeField = ['ctime']; protected $dateFormat = 'Y-m-d H:i:s'; //寫入日期格式 //定義字段輸出格式 protected $type = [ 'ctime' => 'timestamp:Y-m-d H:i:s', 'utime' => 'timestamp:Y-m-d H:i:s', ]; //驗證 protected $rules = [ ['level','number|between:1,9','告警級別有誤|級別必須是1~9級'], ['status','number|between:0,1','狀態|狀態必須是0~1'], ['message','require','信息必須'], ['ip','ip','IP地址有誤'] ]; //自定義初始化 protected static function init() { //TODO:自定義的初始化 } //獲取時自動處理字段轉換 public function getLevelAttr($value){ $Level = [ 0=>'log', 1=>'error', 2=>'notice', 3=>'info', 4=>'debug' ]; return $Level[$value]; } //獲取時自動處理字段轉換 public function getIpAttr($value){ return long2ip($value); } //寫入時自動處理 public function setIpAttr($value){ return ip2long(request()->ip()); } //驗證訪問是否有效 public function write($data){ $validate = new Validate($this->rules); if ($validate->check($data)){ return $this->data($data)->allowField(['message','level','status'])->save(); }else{ return $validate->getError(); } } public function scopeGetLast($query){ $query->order('ctime','desc'); } } ~~~ 寫入數據 ~~~ $Log = model('Log','model'); // return $Log->where('id<781')->delete(); $data = [ 'message' =>'sdf', 'status'=>1, //'ip'=>'192.168.0.26', //系統自動添加 'level'=>4, //'ctime'=>"2016-08-04 15:00", ]; return $Log->write($data); ~~~ 如上,寫入時,model已經定義好,會對ip,ctime,utime字段進行轉換,注意,ctime,utime,model中已定義為自動添加 讀取數據 ~~~ $Log = model('Log','model'); return dump(modelo2array($Log->select())); ~~~ 獲取結果如下圖 紅色字段,已自動進行轉換 ![](https://box.kancloud.cn/2016-08-25_57be99bc3cdf0.png) 附件上modelo2array()函數如下 ~~~ /* * 將model查詢對像轉換為數組數據 * */ function modelo2array($object){ $data = []; foreach($object as $key=>$rowO){ $data[$key] = $rowO->toArray(); } return $data; } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看