iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 22
0
Modern Web

你說Codeigniter還能怎麼做?系列 第 22

[Day22] 通用Models (上)

上次成功加入GETPOST,這次我們要建立一個通用的Models。

有時我們會遇到重複使用新增、修改、刪除的變數,這時我們需要一個通用的功能來達成目的,首先我們先在Models新增Mod_universal.php檔案:

<?php defined('BASEPATH') or exit('No direct script access allowed');

class Mod_universal extends CI_Model {
  public function __construct() {
      parent::__construct();
  }
}

/* End of file Mod_universal.php */

接著我們透過指令新增功能,參考文件可以尋找libraries/Mongo_db.php

確認資料是否存在

  /**
   * 確認資料是否存在
   * verify = 驗證
   * likes = 相似關鍵字
   * table = 資料庫名稱 (必要)
   */
  function chk_once($dataQuery){
    if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
    if (isset($dataQuery['likes'])) {
      foreach ($dataQuery['likes'] as $key => $value) {
          $this->mongo_db->like($key, $value);
      }
    }
    $response = $this->mongo_db->count($dataQuery['table']);
    if ($response == 0) {
      return false;
    } else {
      return true;
    }
  }

取得資料數量

  /**
   * 取得資料數量
   * verify = 驗證
   * likes = 相似關鍵字
   * table = 資料庫名稱 (必要)
   * record = 特殊資料
   */
  function get_count($dataQuery){
    if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
    if (isset($dataQuery['likes'])) {
      foreach ($dataQuery['likes'] as $key => $value) {
        $this->mongo_db->like($key, $value);
      }
    }
    if (isset($dataQuery['record']['between'])) {
      $this->mongo_db->where_between($dataQuery['record']['between']['item'], 
      $dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
    }
    if (isset($dataQuery['record']['where_in'])) {
      $this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
    }
    if (isset($dataQuery['record']['or_where'])) {
      $this->mongo_db->where_or($dataQuery['record']['or_where']);
    }
    if (isset($dataQuery['record']['where_gte'])) {
      $this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
    }
    if (isset($dataQuery['record']['where_lte'])) {
      $this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
    }
    if (isset($dataQuery['record']['order_by'])) {
      $this->mongo_db->order_by($dataQuery['record']['order_by']);
    } else {
      $this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
    }
    if (isset($dataQuery['record']['where_not_in'])) {
      $this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
    }
    $response = $this->mongo_db->count($dataQuery['table']);
    return $response;
  }

取得單一資料

  /**
   * 取得單一資料
   * verify = 驗證
   * likes = 相似關鍵字
   * table = 資料庫名稱 (必要)
   * record = 特殊資料
   */
  function get_once($dataQuery){
    if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
    if (isset($dataQuery['likes'])) {
      foreach ($dataQuery['likes'] as $key => $value) {
        $this->mongo_db->like($key, $value);
      }
    }
    if (isset($dataQuery['record']['between'])) {
      $this->mongo_db->where_between($dataQuery['record']['between']['item'], 
      $dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
    }
    if (isset($dataQuery['record']['where_in'])) {
      $this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
    }
    if (isset($dataQuery['record']['or_where'])) {
      $this->mongo_db->where_or($dataQuery['record']['or_where']);
    }
    if (isset($dataQuery['record']['where_gte'])) {
      $this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
    }
    if (isset($dataQuery['record']['where_lte'])) {
      $this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
    }
    if (isset($dataQuery['record']['order_by'])) {
      $this->mongo_db->order_by($dataQuery['record']['order_by']);
    } else {
      $this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
    }
    if (isset($dataQuery['record']['where_not_in'])) {
      $this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
    }
    $response = $this->mongo_db->find_one($dataQuery['table']);
    return $response;
  }

取得特定資料

  /**
   * 取得特定資料
   * verify = 驗證
   * likes = 相似關鍵字
   * table = 資料庫名稱 (必要)
   * record = 特殊資料
   */
  function get_list($dataQuery){
    if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
    if (isset($dataQuery['likes'])) {
      foreach ($dataQuery['likes'] as $key => $value) {
        $this->mongo_db->like($key, $value);
      }
    }
    if (isset($dataQuery['record']['limit'])) $this->mongo_db->limit($dataQuery['record']['limit']);
    if (isset($dataQuery['record']['page'])) $this->mongo_db->offset($dataQuery['record']['limit']*($dataQuery['record']['page'] - 1));
    if (isset($dataQuery['record']['between'])) {
      $this->mongo_db->where_between($dataQuery['record']['between']['item'], 
      $dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
    }
    if (isset($dataQuery['record']['where_in'])) {
      $this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
    }
    if (isset($dataQuery['record']['or_where'])) {
      $this->mongo_db->where_or($dataQuery['record']['or_where']);
    }
    if (isset($dataQuery['record']['where_gte'])) {
      $this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
    }
    if (isset($dataQuery['record']['where_lte'])) {
      $this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
    }
    if (isset($dataQuery['record']['order_by'])) {
      $this->mongo_db->order_by($dataQuery['record']['order_by']);
    } else {
      $this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
    }
    if (isset($dataQuery['record']['where_not_in'])) {
      $this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
    }
    $response = $this->mongo_db->get($dataQuery['table']);
    return $response;
  }

取得所有特定資料

  /**
   * 取得所有特定資料
   * verify = 驗證
   * likes = 相似關鍵字
   * table = 資料庫名稱 (必要)
   * record = 特殊資料
   */
  function get_all($dataQuery){
    if (isset($dataQuery['verify'])) $this->mongo_db->where($dataQuery['verify']);
    if (isset($dataQuery['likes'])) {
      foreach ($dataQuery['likes'] as $key => $value) {
        $this->mongo_db->like($key, $value);
      }
    }
    if (isset($dataQuery['record']['between'])) {
      $this->mongo_db->where_between($dataQuery['record']['between']['item'], 
      $dataQuery['record']['between']['start'], $dataQuery['record']['between']['end']);
    }
    if (isset($dataQuery['record']['where_in'])) {
      $this->mongo_db->where_in($dataQuery['record']['where_in']['key'], $dataQuery['record']['where_in']['value']);
    }
    if (isset($dataQuery['record']['or_where'])) {
      $this->mongo_db->where_or($dataQuery['record']['or_where']);
    }
    if (isset($dataQuery['record']['where_gte'])) {
      $this->mongo_db->where_gte($dataQuery['record']['where_gte']['key'], $dataQuery['record']['where_gte']['value']);
    }
    if (isset($dataQuery['record']['where_lte'])) {
      $this->mongo_db->where_lte($dataQuery['record']['where_lte']['key'], $dataQuery['record']['where_lte']['value']);
    }
    if (isset($dataQuery['record']['order_by'])) {
      $this->mongo_db->order_by($dataQuery['record']['order_by']);
    } else {
      $this->mongo_db->order_by(array('createAt'=> 'DESC', 'createAtTime'=> 'DESC'));
    }
    if (isset($dataQuery['record']['where_not_in'])) {
      $this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
    }
    $response = $this->mongo_db->get($dataQuery['table']);
    return $response;
  }

基本上透過傳入值來新增內容,其中裡面的table代表特定資料庫是必要的。
今天我們就到這,明天接下去寫新增、刪除、修改。

Next station ... 通用Models


上一篇
[Day21] 改造API GetPost?
下一篇
[Day23] 通用Models (下)
系列文
你說Codeigniter還能怎麼做?30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言