Table of Contents
AR operations in the CI framework, ciar
Home Backend Development PHP Tutorial AR operations in CI framework, ciar_PHP tutorial

AR operations in CI framework, ciar_PHP tutorial

Jul 12, 2016 am 09:07 AM
exists

AR operations in the CI framework, ciar

Part of the code in the Model layer

<span> 1</span>     <span>/*</span><span>*
</span><span> 2</span> <span>     * CI 中的 AR 操作
</span><span> 3</span> <span>     * @author    zhaoyingnan
</span><span> 4</span> <span>     *</span><span>*/</span>
<span> 5</span>     <span>public</span> <span>function</span><span> mAR()
</span><span> 6</span> <span>    {
</span><span> 7</span>         <span>/*</span><span>************** 查询 ************</span><span>*/</span>
<span> 8</span>         <span>//</span><span>select * from mp4ba limit 21,10;
</span><span> 9</span> <span>        //$objResult    =    $this->db->get('mp4ba', 10, 21);
</span><span>10</span> <span>        //echo $this->db->last_query();die;
</span><span>11</span> 
<span>12</span> 
<span>13</span> <span>        //select * from mp4ba where id =32 limit 21,10;
</span><span>14</span> <span>        //select * from mp4ba where id =32 and name = '刺客聂隐娘'limit 21,10;
</span><span>15</span> <span>        //$objResult    =    $this->db->get_where('mp4ba', array('id'=>32), 10, 21);
</span><span>16</span> <span>        //echo $this->db->last_query();die;
</span><span>17</span> <span>        //$objResult    =    $this->db->get_where('mp4ba', array('id'=>32,'name'=>'刺客聂隐娘'), 10, 21);
</span><span>18</span> <span>        //echo $this->db->last_query();die;
</span><span>19</span> 
<span>20</span> 
<span>21</span> <span>        //select id,name,url from mp4ba where id =32;
</span><span>22</span> <span>        //$objResult    =    $this->db->select('id,name,url')->get_where('mp4ba', array('id'=>32));
</span><span>23</span> <span>        //echo $this->db->last_query();die;
</span><span>24</span> 
<span>25</span> <span>        //select id,name,url from mp4ba where id =32 or id=39;
</span><span>26</span> <span>        //$objResult    =    $this->db->select('id,name,url')->where(array('id'=>32))->or_where(array('id'=>39))->get('mp4ba');
</span><span>27</span> <span>        //echo $this->db->last_query();die;
</span><span>28</span>         
<span>29</span> 
<span>30</span> <span>        //select id,name,url from mp4ba where id in(33,44,55);
</span><span>31</span> <span>        //select id,name,url from mp4ba where id in(33,44,55) or sort_id in (3,4);
</span><span>32</span> <span>        //select id,name,url from mp4ba where id not in(33,44,55);
</span><span>33</span> <span>        //$objResult    =    $this->db->select('id,name,url')->where_in('id', array(33,44,55))->get('mp4ba');
</span><span>34</span> <span>        //$objResult    =    $this->db->select('id,name,url')->where_in('id', array(33,44,55))->or_where_in('sort_id', array(3,4))->get('mp4ba');
</span><span>35</span> <span>        //$objResult    =    $this->db->select('id,name,url')->where_not_in('id', array(33,44,55))->get('mp4ba');
</span><span>36</span> <span>        //echo $this->db->last_query();die;
</span><span>37</span> 
<span>38</span> <span>        //select id,name,url from mp4ba join user on (mp4ba.uid=user.id) order by mp4ba.dateline desc;
</span><span>39</span> <span>        //$objResult    =    $this->db->select('id,name,url')->from('mp4ba')->join('user', 'mp4ba.uid = user.id')->order_by('mp4ba.dateline', 'desc')->get();
</span><span>40</span> <span>        //echo $this->last_query();die;
</span><span>41</span> 
<span>42</span> 
<span>43</span> <span>        //select * from mp4ba where name like '%刺客%';
</span><span>44</span> <span>        //select * from mp4ba where name not like '%刺客%';
</span><span>45</span> <span>        //select * from mp4ba where name like '%刺客%' or url like 'eqfdf%';
</span><span>46</span> <span>        //$objResult    =    $this->db->like('name', '刺客')->get('mp4ba');
</span><span>47</span> <span>        //$objResult    =    $this->db->not_like('name', '刺客')->get('mp4ba');
</span><span>48</span> <span>        //$objResult    =    $this->db->like('name', '刺客')->or_like('url', 'eqfdf', 'after')->get('mp4ba');
</span><span>49</span> <span>        //echo $this->db->last_query();die;
</span><span>50</span> 
<span>51</span> 
<span>52</span> 
<span>53</span> <span>        //select max(id) from mp4ba where name = '刺客聂隐娘';
</span><span>54</span> <span>        //select min(id) from mp4ba where name = '刺客聂隐娘';
</span><span>55</span> <span>        //$objResult    =    $this->db->select_max('id')->get_where('mp4ba', array('name'=>'刺客聂隐娘'));
</span><span>56</span> <span>        //echo $this->db->last_query();die;
</span><span>57</span> <span>        //$objResult    =    $this->db->select_min('id')->get_where('mp4ba', array('name'=>'刺客聂隐娘'));
</span><span>58</span> <span>        //echo $this->db->last_query();die;
</span><span>59</span> 
<span>60</span> <span>        //SELECT id,sort_id,menu,name FROM mp4ba WHERE id > 3 ORDER BY `dateline` desc LIMIT 10,100
</span><span>61</span> <span>        //$objResult    =    $this->db->select('id,sort_id,menu,name')->from('mp4ba')->where('id >', 3)->order_by('dateline desc')->limit(100,10)->get();
</span><span>62</span> <span>        //echo $this->db->last_query();
</span><span>63</span> <span>        //return $objResult->result();</span>
<span>64</span> 
<span>65</span> 
<span>66</span>         <span>/*</span><span>************** 插入 ************</span><span>*/</span>
<span>67</span>         <span>//</span><span>生成一条基于你所提供的数据的SQL插入字符串并执行查询。你可以向函数传递 数组 或一个 对象。下面是一个使用数组的例子:</span>
<span>68</span>         <span>$arInsert</span>    =    <span>array</span><span>(
</span><span>69</span>             'name'        =>    '小黄人',
<span>70</span>             'url'        =>    'www.test.com',
<span>71</span>             'sort_id'    =>    1,
<span>72</span>             'menu'        =>    '动画片'
<span>73</span> <span>        );
</span><span>74</span>         <span>//</span><span>$this->db->insert('mp4ba', $arInsert);
</span><span>75</span> <span>        //echo $this->db->insert_id();die;</span>
<span>76</span> 
<span>77</span> 
<span>78</span>         <span>/*</span><span>************** 修改 ************</span><span>*/</span>
<span>79</span>         <span>$arData</span>    =    <span>array</span><span>(
</span><span>80</span>             'name'        =>    '小黄人,好玩嘛',
<span>81</span>             'url'        =>    'www.test_xiaohuangren.com',
<span>82</span>             'sort_id'    =>    1,
<span>83</span>             'menu'        =>    '动画片'
<span>84</span> <span>        );
</span><span>85</span>         <span>//</span><span>$this->db->update('mp4ba', $arData, array('id'=>3498));
</span><span>86</span> <span>        //echo $this->db->affected_rows();    #受影响的行数
</span><span>87</span> <span>        //echo '<br/>';
</span><span>88</span> <span>        //$objResult    =    $this->db->where(array('id'=>3498))->get('mp4ba');
</span><span>89</span> <span>        //formatOut($objResult->result());die;</span>
<span>90</span>         
<span>91</span>         <span>/*</span><span>************** 删除 ************</span><span>*/</span>
<span>92</span>         <span>$this</span>->db->delete('mp4ba', <span>array</span>('id'=>3498<span>));
</span><span>93</span>         <span>echo</span> <span>$this</span>->db->affected_rows();    <span>#</span><span>受影响的行数</span>
<span>94</span>     }
Copy after login

Annotations for some methods in the DB_active_rec.php class in CI (will continue to be added)

AR operations in CI framework, ciar_PHP tutorial 1 php 2 class CI_DB_active_record 3 { 4 /** 5 * get 6 * @author zhaoyingnan 2015-10-14 12:50 7 * @param string $table 操作的表 8 * @param int $limit limit 值 9 * @param int $offset offset 值 10 * @return object 11 **/ 12 public function get($table = '', $limit = null, $offset = null) 13 {} 14 15 /** 16 * get_where 17 * @author zhaoyingnan 2015-10-14 12:58 18 * @param string $table 操作的表 19 * @param array $where where 子句 20 * @param int $limit limit 值 21 * @param int $offset offset 值 22 * @return object 23 **/ 24 public function get_where($table = '', $where = null, $limit = null, $offset = null) 25 {} 26 27 /** 28 * select 29 * @author zhaoyingnan 2015-10-14 13:13 30 * @param string $select 查询的字段,用逗号隔开 31 * @param boolean $escape 如果你把它设为FALSE, CodeIgniter 将不会使用反引号保护你的字段或者表名 。这在进行复合查询时很有用。 32 * @return object 33 **/ 34 public function select($select = '*', $escape = NULL) 35 {} 36 37 /** 38 * SELECT MAX(field) portion of a query* @description 39 * @author zhaoyingnan 2015-10-14 13:20 40 * @param string $select max(field)作用列 41 * @param string $alias 别名 42 * @return object 43 **/ 44 public function select_max($select = '', $alias = '') 45 {} 46 47 /** 48 * SELECT MIN(field) portion of a query 49 * @author zhaoyingnan 2015-10-14 13:20 50 * @param string $select min(field)作用列 51 * @param string $alias 别名 52 * @return object 53 **/ 54 public function select_min($select = '', $alias = '') 55 {} 56 57 /** 58 * SELECT AVG(field) portion of a query 59 * @author zhaoyingnan 2015-10-14 13:20 60 * @param string $select AVG(field)作用列 61 * @param string $alias 别名 62 * @return object 63 **/ 64 public function select_avg($select = '', $alias = '') 65 {} 66 67 /** 68 * SELECT SUM(field) portion of a query 69 * @author zhaoyingnan 2015-10-14 13:20 70 * @param string $select SUM(field)作用列 71 * @param string $alias 别名 72 * @return object 73 **/ 74 public function select_sum($select = '', $alias = '') 75 {} 76 77 78 /** 79 * @description 80 * @author zhaoyingnan 2015-10-14 13:26 81 * @param string $from 表名 82 * @return object 83 **/ 84 public function from($from) 85 {} 86 87 /** 88 * where 89 * @author zhaoyingnan 2015-10-14 13:31 90 * @param mix $key passes the key value of the array 91 * @param mix $value passes the value corresponding to the key of the array 92 * @return object 93 **/ 94 public function where($key, $value = NULL, $escape = TRUE) 95 { 96 //1, simple key/value method: 97 //$this->db->where('name', $name); 98 //Generate: WHERE name = 'Joe' 99 100 //2. Custom key/value method: 101 //$this->db->where('name !=', $name); 102 //$this->db->where('id <', $id); 103 //Generate: WHERE name != 'Joe' AND id < 45 104 105 //3. Associative array method: 106 //$array = array('name' => $name, 'title' => $title, 'status' => $status); 107 //$this->db->where($array); 108 //Generate: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' 109 //You can also include operators when using this method: 110 //$array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); 111 112 //4. Custom string: 113 //$where = "name='Joe' AND status='boss' OR status='active'"; 114 //$this->db->where($where); 115 return $this->_where($key, $value, 'AND ', $escape); 116 } 117 118 /** 119 * where 120 * @author zhaoyingnan 2015-10-14 13:31 121 * @param mix $key passes the key value of the array 122 * @param mix $value passes the value corresponding to the key of the array 123 * @return object 124 **/ 125 public function or_where($key, $value = NULL, $escape = TRUE) 126 { 127 //Reference where 128 return $this->_where($key, $value, 'OR ', $escape); 129 } 130 131 132 /** 133 * where_in 134 * @author zhaoyingnan 2015-10-14 13:58 135 * @param string $key The column to be queried 136 * @param string $values ​​The value range of the column 137 * @return object 138 **/ 139 public function where_in($key = NULL, $values = NULL) 140 { 141 return $this->_where_in($key, $values ); 142 }143 144 /** 145 * or_where_in 146 * @author zhaoyingnan 2015-10-14 13:58 147 * @param string $key The column to be queried 148 * @param string $values ​​The value range of the column 149 * @return object 150 **/ 151 public function or_where_in($key = NULL, $values = NULL) 152 { 153 return $this->_where_in($key, $values, FALSE, 'OR '); 154 } 155 156 /** 157 * where_not_in 158 * @author zhaoyingnan 2015-10-14 13:58 159 * @param string $key The column to be queried 160 * @param string $values ​​The value range of the column 161 * @return object 162 **/ 163 public function where_not_in($key = NULL, $values = NULL) 164 { 165 return $this->_where_in($key, $values, TRUE); 166 } 167 168 /** 169 * or_where_not_in 170 * @author zhaoyingnan 2015-10-14 13:58 171 * @param string $key The column to be queried 172 * @param string $values ​​The value range of the column 173 * @return object 174 **/ 175 public function or_where_not_in($key = NULL, $values = NULL) 176 { 177 return $this->_where_in($key, $values, TRUE, 'OR '); 178 } 179 180 /** 181 * order by 182 * @author zhaoyingnan 2015-10-14 13:35 183 * @param string $orderby the column to be sorted 184 * @param string $direction asc or desc 185 * @return object 186 **/ 187 public function order_by($orderby, $direction = '') 188 {} 189 190 /** 191 * join 192 * @author zhaoyingnan 2015-10-14 14:07 193 * @param string $table table name 194 * @param string $cond condition 195 * @param string $type Specifies the type of JOIN. Options include: left, right, outer, inner, left outer, and right outer 196 * @return 197 **/ 198 public function join($table, $cond, $type = '') 199 {}200 201 /** 202 * like 203 * @author zhaoyingnan 2015-10-14 14:28 204 * @param stringi $field wrong column 205 * @param mix $match rule 206 * @param mix $side wildcard (%) position Available options are 'before', 'after' and 'both' (this is the default) 207 * @return object 208 **/ 209 public function like($field, $match = '', $side = 'both') 210 { 211 return $this->_like($field, $match, 'AND ', $side); 212 } 213 214 /** 215 * not_like 216 * @author zhaoyingnan 2015-10-14 14:28 217 * @param stringi $field wrong column 218 * @param mix $match rule 219 * @param mix $side wildcard (%) position Available options are 'before', 'after' and 'both' (this is the default) 220 * @return object 221 **/ 222 public function not_like($field, $match = '', $side = 'both') 223 { 224 return $this->_like($field, $match, 'AND ', $side, 'NOT'); 225 } 226 227 /** 228 * or_like 229 * @author zhaoyingnan 2015-10-14 14:28 230 * @param stringi $field wrong column 231 * @param mix $match rule 232 * @param mix $side wildcard (%) position Available options are 'before', 'after' and 'both' (this is the default) 233 * @return object 234 **/ 235 public function or_like($field, $match = '', $side = 'both') 236 { 237 return $this->_like($field, $match, 'OR ', $side); 238 } 239 240 /** 241 * or_not_like 242 * @author zhaoyingnan 2015-10-14 14:28 243 * @param stringi $field wrong column 244 * @param mix $match rule 245 * @param mix $side wildcard (%) position Available options are 'before', 'after' and 'both' (this is the default) 246 * @return object 247 **/ 248 public function or_not_like($field, $match = '', $side = 'both') 249 { 250 return $this->_like($field, $match, 'OR ', $side, 'NOT'); 251 } 252 253 /** 254 * insert single insert 255 * @author zhaoyingnan 2015-10-14 14:52 256 * @param string $table table name 257 * @param array $set associative array 258 * @return object 259 **/ 260 function insert($table = '', $set = NULL) 261 {}262 263 /** 264 * insert batch entry and exit 265 * @author zhaoyingnan 2015-10-14 14:52 266 * @param string $table table name 267 * @param array $set associative array 268 * @return object 269 **/ 270 public function insert_batch($table = '', $set = NULL) 271 {} 272 273 /** 274 * update 275 * @author zhaoyingnan 2015-10-14 15:02 276 * @param string $table table name 277 * @param array $set associative array to modify the content 278 * @param mixed $where where condition 279 * @return object 280 **/ 281 public function update($table = '', $set = NULL, $where = NULL, $limit = NULL) 282 {} 283 284 /** 285 * delete 286 * @author zhaoyingnan 2015-10-14 15:12 287 * @param mix $table table name 288 * @param mixed $where where condition 289 * @return object 290 **/ 291 public function delete($table = '', $where = '', $limit = NULL, $reset_data = TRUE) 292 { 293 //The first parameter is the table name, and the second parameter is the where clause. You can omit the second argument and use the where() or or_where() function instead: 294 //$this->db->where('id', $id); 295 //$this->db->delete('mytable'); 296 297 298 //If you want to delete data from more than one table, you can pass an array containing multiple table names to the delete() function. 299 //$tables = array('table1', 'table2', 'table3'); 300 //$this->db->where('id', '5'); 301 //$this->db->delete($tables); 302 303 304 //If you want to delete all the data in the table, you can use the truncate() function, or the empty_table() function. 305 } 306 307 /** 308 * limit 309 * @author zhaoyingnan 2015-10-14 14:34 310 * @param int $value 311 * @param int $offset 312 * @return object 313 **/ 314 public function limit($value, $offset = '') 315 {} 316 317 318 319 /** 320 * Where 321 * 322 * Called by where() or or_where() 323 * 324 * @param mixed 325 * @param mixed 326 * @param string 327 * @return object 328 **/ 329 protected function _where($key, $value = NULL, $type = 'AND ', $escape = NULL) 330 {} 331 332 /** 333 * Like 334 * 335 * Called by like() or orlike() 336 * 337 * @param mixed 338 * @param mixed 339 * @param string 340 * @return object 341 **/ 342 protected function _like($field, $match = '', $type = 'AND ', $side = 'both', $not = '') 343 { 344 if ( ! is_array($field)) 345 { 346 $field = array($field => $match); 347 } 348 349 foreach ($field as $k => $v) 350 { 351 $k = $this->_protect_identifiers($k); 352 353 $prefix = (count($this->ar_like) == 0) ? '' : $type; 354 355 $v = $this->escape_like_str($v); 356 357 if ($side == 'none') 358 { 359 $like_statement = $prefix." $k $not LIKE '{$v}'"; 360 } 361 elseif ($side == 'before') 362 { 363 $like_statement = $prefix." $k $not LIKE '%{$v}'"; 364 } 365 elseif ($side == 'after') 366 { 367 $like_statement = $prefix." $k $not LIKE '{$v}%'"; 368 } 369 else 370 { 371 $like_statement = $prefix." $k $not LIKE '%{$v}%'"; 372 }373 374 // some platforms require an escape sequence definition for LIKE wildcards 375 if ($this->_like_escape_str != '') 376 { 377 $like_statement = $like_statement.sprintf($this->_like_escape_str, $this->_like_escape_chr); 378 } 379 380 $this->ar_like[] = $like_statement; 381 if ($this->ar_caching === TRUE) 382 { 383 $this->ar_cache_like[] = $like_statement; 384 $this->ar_cache_exists[] = 'like'; 385 } 386 387 } 388 return $this; 389 } 390 391 } View Code

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1060219.htmlTechArticleCI 框架中 AR 操作,ciar Model 层中的部分代码 1 /* * 2 * CI 中的 AR 操作 3 * @author zhaoyingnan 4 * */ 5 public function mAR() 6 { 7 /* ************** 查询 *****...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

See all articles