一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - PHP教程 - CI框架(CodeIgniter)公共模型類定義與用法示例

CI框架(CodeIgniter)公共模型類定義與用法示例

2021-06-15 14:13kangjianrong PHP教程

這篇文章主要介紹了CI框架(CodeIgniter)公共模型類定義與用法,結(jié)合具體實(shí)例形式分析了CI框架公共模型類的定義以及基于公共模型類操作數(shù)據(jù)庫的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下

本文實(shí)例講述了CI框架(CodeIgniter)公共模型類定義與用法。分享給大家供大家參考,具體如下:

我們都知道,操作數(shù)據(jù)庫的方法都寫在模型中。但是一般情況下,一張表往往至少對(duì)應(yīng)4個(gè)操作,也就是所謂crud。那么如果20張表,所對(duì)應(yīng)的模型方法,就達(dá)到了80個(gè),重復(fù)的操作顯然這已經(jīng)是一個(gè)體力活兒。

那么就對(duì)單表操作時(shí),我們進(jìn)行一下簡單的封裝。如下是ci框架的示例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
/**
 * Created by PhpStorm.
 * User: kangjianrong
 * Date: 16-8-26
 * Time: 上午10:29
 */
class My_model extends CI_Model {
  //數(shù)據(jù)庫
  public $errors = array();
  const dataBase = 'qndnew';
  public function __construct()
  {
    // Call the CI_Model constructor
    parent::__construct();
  }
  /**
   * 查詢分頁數(shù)據(jù)(使用于簡單的單表操作)
   * @param string $model 模型     例如:User_model
   * @param string $table 表名
   * @param string $select_fields 要顯示字段
   * @param array $param 查詢條件:
   *   compare(比較):
   *     array($key => $val) $key為要操作的字段,$val為要操作的值
   *     array('name !=' => $name, 'id <' => $id, 'date >' => $date);
   *   like(模糊查詢)
   *     array('title' => $match, 'page1' => $match, 'page2' => $match)
   *   customStr(自定義字符串):
   *     "name='Joe' AND status='boss' OR status='active'"
   *   in:
   *     array('userName' => array('Frank', 'Todd', 'James'))
   * @param string $page 當(dāng)前頁數(shù)(查詢?nèi)繑?shù)據(jù)時(shí),設(shè)置為空)
   * @param string $limit 查詢條數(shù)(查詢?nèi)繑?shù)據(jù)時(shí),設(shè)置為空)
   * @param array $order 排序條件:
   *   array($key => $val)
   *   $key為排序依據(jù)的字段,
   *   $val為排序的方式【asc (升序,默認(rèn))或 desc(降序), 或 random(隨機(jī))】
   * @$isReturnCount boole    是否返回總條數(shù)
   * @return array|boolean
   *
   */
  public function pageData($model, $table, $param = array(),$select_fields = '', $page = '1', $limit = '15', $order = array(),$isReturnCount = true){
    if(empty($model) || empty($table)){
      return false;
    }
    $this -> load -> model($model);
    $table = $this->db->dbprefix.$table;
    //處理查詢字段
    if(!empty($select_fields)){
      $this->db->select($select_fields)->from($table);
    }elseif(isset($this -> $model -> selectFields)){
      $this->db->select($this -> $model -> selectFields)->from($table);
    }else{
      $this->db->select('*')->from($table);
    }
    //處理查詢條件
    if (is_array($param) && count($param) > 0){
      $this -> parseParam($param);
    }
    //統(tǒng)計(jì)總數(shù)
    if($isReturnCount){
      $rs['count']  = $this->db->count_all_results('',false);//不重置查詢構(gòu)造器
      array_push($this -> errors,$this->db->last_query());
    }
    //分頁數(shù)據(jù)處理
    if(isset($page) && isset($param['limit'])){
      //分頁邊界值 設(shè)置
      $offset = $param['page'] <= 1 ? 0 : ($param['page']-1) * $param['limit'];
      $this->db->limit($param['limit'], $offset);
    }
    //排序規(guī)則的組合
    if (!empty($order) && is_array($order))
    {
      foreach ($order as $key => $val)
      {
        $this->db->order_by($key, $val);
      }
    }else{
      //默認(rèn)按照此表的主鍵倒序
      $primary = $this->getPrimary();
      if(!empty($primary))
      {
        $this->db->order_by($primary, 'DESC');
      }
    }
    $query = $this->db->get();
    array_push($this -> errors,$this->db->last_query());
    $rs['list'] = $query->result_array();
    return $rs;
  }
  /**
   * 解析參數(shù)
   */
  private function parseParam($param){
    if(isset($param['compare'])){
      foreach ($param['compare'] as $key => $val){
        if (!empty($val)) $this->db->where($key, $val);
      }
    }
    if(isset($param['like'])){
      foreach ($param['like'] as $key => $val){
        if (!empty($val)) $this->db->like($key, $val);
      }
    }
    if(isset($param['in'])){
      foreach ($param['in'] as $key => $val){
        if (!empty($val)) $this->db->where_in($key, $val);
      }
    }
    if(isset($param['customStr'])){
      if (!empty($val)) $this->db->where($param['customStr']);
    }
  }
  /**
   * 新增信息
   * @param string $table 表名稱
   * @param array $param 數(shù)據(jù)變量
   * @return INT ID
   */
  public function add($table = '', $param = array())
  {
    if(empty($table) || !is_array($param) || empty ($param)){
      return FALSE;
    }
    //寫入數(shù)據(jù)表
    $this->db->insert($table, $param);
      array_push($this -> errors,$this->db->last_query());
    //返回記錄ID
    return $this->db->insert_id();
  }
  /**
   * 更新分類信息
   * @param string  $table   表名稱
   * @param string  $primary  表主鍵
   * @param int    $id     分類ID
   * @param array   $param   更新的數(shù)據(jù)
   * @return type
   */
  public function update($table = '', $primary = '', $id = 0, $param = array())
  {
    if(empty($table) || empty($primary) || empty($param) || empty($id))
    {
      return FALSE;
    }
    $id = (int)$id;
    $this->db->where($primary, $id)
         ->limit(1)
         ->update($table, $param);
    array_push($this -> errors,$this->db->last_query());
    return $this->db->affected_rows();
  }
  /**
   * 刪除指定ID記錄
   * @param string  $table   表名稱
   * @param string  $primary  表主鍵
   * @param array   $id     分類ID
   * @return int
   */
  public function delete($table = '', $primary = '', $id = array()){
    if(empty($table) || empty($primary) || empty($id)){
      return FALSE;
    }
    $this->db->where_in($primary, $id)
        ->delete($table);
    array_push($this -> errors,$this->db->last_query());
    return $this->db->affected_rows();
  }
  /**
   * 獲取表的主鍵
   * @param string  $database  數(shù)據(jù)庫名稱
   * @param strting  $table   表名稱
   */
  public function getPrimary($table = '', $database = self::dataBase)
  {
    if(empty($database) || empty($table))
    {
      return FALSE;
    }
    $sql = "SELECT k.column_name
        FROM information_schema.table_constraints t
        JOIN information_schema.key_column_usage k
        USING (constraint_name,table_schema,table_name)
        WHERE t.constraint_type='PRIMARY KEY'
         AND t.table_schema='qndnew'
         AND t.table_name='qnd_user'";
    $query = $this->db->query($sql)->result_array();
    return isset($query[0]['column_name']) ? $query[0]['column_name'] : false;
  }
  /**
   * debug sql語句
   */
  public function debugSql(){
    if(count($this->errors) > 0){
      foreach($this->errors as $val){
        echo $val.'<br>';
      }
    }
  }
}

具體的業(yè)務(wù)邏輯模型如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class User_model extends My_model {
  const USER = 'qnd_user';
  public $selectFields = array(
    'id',
    'guid',
    'phone',
    'userName',
    'password',
    'headPortraits',
    'nickName',
    'createTime',
  );
  const SMS_ROLE = 'qnd_role';
  public function __construct()
  {
  }
}

控制器中測試如下:

?
1
2
3
4
5
6
7
8
9
10
11
public function modelTest(){
    $this -> load -> model('User_model'); // 載入 model
    $whereArr = array(
            'compare'=>array(
              'userName' => 'Frank',
            ),
          );
    $rs = $this -> User_model -> pageData('User_model','user',$whereArr);
    print_r($rs);
    $this -> User_model -> debugSql();
  }

希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。

原文鏈接:http://www.cnblogs.com/kangjianrong/p/5865442.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 韩国免费特一级毛片 | 亚洲人的天堂男人爽爽爽 | 日韩欧美三级视频 | 亚洲AV精品一区二区三区不卡 | 高清一级做a爱免费视 | 强制高h| 日韩免费视频播放 | a级毛片毛片免费很很综合 a级黄色视屏 | 男人的天堂久久精品激情 | 15同性同志18 | 美女被网站 | 国产精品99爱免费视频 | 无限时间看片在线观看 | 国内精品久久久久久久久 | 97久久精品午夜一区二区 | 免费观看伦理片 | 免费国产在线观看 | 为什么丈夫插我我却喜欢被打着插 | 91东航翘臀女神在线播放 | 无人影院在线播放视频 | 国产在线视频色综合 | 国产区成人综合色在线 | 亚洲aⅴ男人的天堂在线观看 | 秋霞在线观看成人高清视频51 | 天天综合网网欲色 | 欧美日韩精品乱国产 | 日韩一级片在线免费观看 | 性欧美4khdxxxx | 精品国产一区二区三区久久久蜜臀 | 波多野结衣被绝伦强在线观看 | 四虎影视在线看 | 亚洲视频999 | 午夜精品久久久久久久2023 | 亚洲精品一区波多野结衣 | 女子监狱第二季在线观看免费完整版 | 精品视频免费在线观看 | 午夜伦理电影在线观免费 | 厨房里摸着乳丰满在线观看 | 99在线在线视频免费视频观看 | 经典WC女厕所里TV | 欧美另类bbbxxxxx另类 |