iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 23
0

上次我們已經完成一部分功能,今日繼續接下去製作新增刪除修改,首先我們先打開Mod_universal.php

新增資料

  /**
   * 新增資料
   * data = 傳入的資料內容
   * table = 資料庫名稱 (必要)
   */
  function insert($dataQuery) {
    $dataQuery['data']['createAt'] = date('Y-m-d');
    $dataQuery['data']['createAtTime'] = date('H:i:s');
    $dataQuery['data']['tsCreateAt'] = time();
    $dataQuery['data']['updateAt'] = date('Y-m-d');
    $dataQuery['data']['updateAtTime'] = date('H:i:s');
    $dataQuery['data']['tsUpdateAt'] = time();
    $response = $this->mongo_db->insert($dataQuery['table'], $dataQuery['data']);
    return $response;
  }

更新資料

  /**
   * 更新資料
   * verify = 驗證
   * likes = 相似關鍵字
   * data = 傳入的資料內容
   * table = 資料庫名稱 (必要)
   * record = 特殊資料
   */
  function update($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']['where_not_in'])) {
      $this->mongo_db->where_not_in($dataQuery['record']['where_not_in']['key'], $dataQuery['record']['where_not_in']['value']);
    }
    if (isset($dataQuery['record']['push'])) {
      $this->mongo_db->push($dataQuery['record']['push']['key'], $dataQuery['record']['push']['value']);
    }
    if (isset($dataQuery['record']['pull'])) {
      $this->mongo_db->pull($dataQuery['record']['pull']['key'], $dataQuery['record']['pull']['value']);
    }
    $this->mongo_db->set($dataQuery['data']);
    $response = $this->mongo_db->update_all($dataQuery['table']);
    return $response;
  }

刪除指定資料

  /**
   * 刪除指定資料
   * verify = 寫入的條件式
   * table = 資料庫名稱
   */
  function delete($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']['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->delete($dataQuery['table']);
    return $response;
  }

刪除全部資料

  /**
   * 刪除全部資料
   * verify = 寫入的條件式
   * table = 資料庫名稱
   */
  function delete_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']['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->delete_all($dataQuery['table']);
    return $response;
  }

取得特定欄位

  /**
   * 取得特定欄位.
   * verify = 寫入的條件式
   * table = 資料庫名稱
   */
  function display_word($word, $where, $table) {
    $this->mongo_db->where($where);
    $res = $this->mongo_db->find_one($table);
    return $res[$word];
  }

如此一來就大功告成!

Next station ... Restful API


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

尚未有邦友留言

立即登入留言