Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ################### |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 2 | Query Builder Class |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 3 | ################### |
| 4 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 5 | CodeIgniter gives you access to a Query Builder class. This pattern |
| 6 | allows information to be retrieved, inserted, and updated in your |
| 7 | database with minimal scripting. In some cases only one or two lines |
| 8 | of code are necessary to perform a database action. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 9 | CodeIgniter does not require that each database table be its own class |
| 10 | file. It instead provides a more simplified interface. |
| 11 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 12 | Beyond simplicity, a major benefit to using the Query Builder features |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | is that it allows you to create database independent applications, since |
| 14 | the query syntax is generated by each database adapter. It also allows |
| 15 | for safer queries, since the values are escaped automatically by the |
| 16 | system. |
| 17 | |
| 18 | .. note:: If you intend to write your own queries you can disable this |
| 19 | class in your database config file, allowing the core database library |
| 20 | and adapter to utilize fewer resources. |
| 21 | |
| 22 | .. contents:: Page Contents |
| 23 | |
| 24 | ************** |
| 25 | Selecting Data |
| 26 | ************** |
| 27 | |
| 28 | The following functions allow you to build SQL **SELECT** statements. |
| 29 | |
| 30 | $this->db->get() |
| 31 | ================ |
| 32 | |
| 33 | Runs the selection query and returns the result. Can be used by itself |
| 34 | to retrieve all records from a table:: |
| 35 | |
| 36 | $query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable |
| 37 | |
| 38 | The second and third parameters enable you to set a limit and offset |
| 39 | clause:: |
| 40 | |
| 41 | $query = $this->db->get('mytable', 10, 20); |
| 42 | // Produces: SELECT * FROM mytable LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax) |
| 43 | |
| 44 | You'll notice that the above function is assigned to a variable named |
| 45 | $query, which can be used to show the results:: |
| 46 | |
| 47 | $query = $this->db->get('mytable'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 48 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 49 | foreach ($query->result() as $row) |
| 50 | { |
| 51 | echo $row->title; |
| 52 | } |
| 53 | |
| 54 | Please visit the :doc:`result functions <results>` page for a full |
| 55 | discussion regarding result generation. |
| 56 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 57 | $this->db->get_compiled_select() |
| 58 | ================================ |
| 59 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 60 | Compiles the selection query just like `$this->db->get()`_ but does not *run* |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 61 | the query. This method simply returns the SQL query as a string. |
| 62 | |
| 63 | Example:: |
| 64 | |
| 65 | $sql = $this->db->get_compiled_select('mytable'); |
| 66 | echo $sql; |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 67 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 68 | // Produces string: SELECT * FROM mytable |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 69 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 70 | The second parameter enables you to set whether or not the query builder query |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 71 | will be reset (by default it will be—just like `$this->db->get()`):: |
| 72 | |
| 73 | echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 74 | // Produces string: SELECT * FROM mytable LIMIT 20, 10 |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 75 | // (in MySQL. Other databases have slightly different syntax) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 76 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 77 | echo $this->db->select('title, content, date')->get_compiled_select(); |
| 78 | |
| 79 | // Produces string: SELECT title, content, date FROM mytable |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 80 | |
| 81 | The key thing to notice in the above example is that the second query did not |
| 82 | utilize `$this->db->from()`_ and did not pass a table name into the first |
| 83 | parameter. The reason for this outcome is because the query has not been |
| 84 | executed using `$this->db->get()`_ which resets values or reset directly |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame] | 85 | using `$this->db->reset_query()`_. |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 86 | |
Derek Jones | 84bcc6e | 2011-10-17 21:24:27 -0500 | [diff] [blame] | 87 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 88 | $this->db->get_where() |
| 89 | ====================== |
| 90 | |
| 91 | Identical to the above function except that it permits you to add a |
| 92 | "where" clause in the second parameter, instead of using the db->where() |
| 93 | function:: |
| 94 | |
| 95 | $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset); |
| 96 | |
| 97 | Please read the about the where function below for more information. |
| 98 | |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame] | 99 | .. note:: get_where() was formerly known as getwhere(), which has been removed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 100 | |
| 101 | $this->db->select() |
| 102 | =================== |
| 103 | |
| 104 | Permits you to write the SELECT portion of your query:: |
| 105 | |
| 106 | $this->db->select('title, content, date'); |
| 107 | $query = $this->db->get('mytable'); // Produces: SELECT title, content, date FROM mytable |
| 108 | |
| 109 | |
| 110 | .. note:: If you are selecting all (\*) from a table you do not need to |
| 111 | use this function. When omitted, CodeIgniter assumes you wish to SELECT * |
| 112 | |
| 113 | $this->db->select() accepts an optional second parameter. If you set it |
| 114 | to FALSE, CodeIgniter will not try to protect your field or table names |
| 115 | with backticks. This is useful if you need a compound select statement. |
| 116 | |
| 117 | :: |
| 118 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 119 | $this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 120 | $query = $this->db->get('mytable'); |
| 121 | |
| 122 | |
| 123 | $this->db->select_max() |
| 124 | ======================= |
| 125 | |
| 126 | Writes a "SELECT MAX(field)" portion for your query. You can optionally |
| 127 | include a second parameter to rename the resulting field. |
| 128 | |
| 129 | :: |
| 130 | |
| 131 | $this->db->select_max('age'); |
| 132 | $query = $this->db->get('members'); // Produces: SELECT MAX(age) as age FROM members |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 133 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 134 | $this->db->select_max('age', 'member_age'); |
| 135 | $query = $this->db->get('members'); // Produces: SELECT MAX(age) as member_age FROM members |
| 136 | |
| 137 | |
| 138 | $this->db->select_min() |
| 139 | ======================= |
| 140 | |
| 141 | Writes a "SELECT MIN(field)" portion for your query. As with |
| 142 | select_max(), You can optionally include a second parameter to rename |
| 143 | the resulting field. |
| 144 | |
| 145 | :: |
| 146 | |
| 147 | $this->db->select_min('age'); |
| 148 | $query = $this->db->get('members'); // Produces: SELECT MIN(age) as age FROM members |
| 149 | |
| 150 | |
| 151 | $this->db->select_avg() |
| 152 | ======================= |
| 153 | |
| 154 | Writes a "SELECT AVG(field)" portion for your query. As with |
| 155 | select_max(), You can optionally include a second parameter to rename |
| 156 | the resulting field. |
| 157 | |
| 158 | :: |
| 159 | |
| 160 | $this->db->select_avg('age'); |
| 161 | $query = $this->db->get('members'); // Produces: SELECT AVG(age) as age FROM members |
| 162 | |
| 163 | |
| 164 | $this->db->select_sum() |
| 165 | ======================= |
| 166 | |
| 167 | Writes a "SELECT SUM(field)" portion for your query. As with |
| 168 | select_max(), You can optionally include a second parameter to rename |
| 169 | the resulting field. |
| 170 | |
| 171 | :: |
| 172 | |
| 173 | $this->db->select_sum('age'); |
| 174 | $query = $this->db->get('members'); // Produces: SELECT SUM(age) as age FROM members |
| 175 | |
| 176 | |
| 177 | $this->db->from() |
| 178 | ================= |
| 179 | |
| 180 | Permits you to write the FROM portion of your query:: |
| 181 | |
| 182 | $this->db->select('title, content, date'); |
| 183 | $this->db->from('mytable'); |
| 184 | $query = $this->db->get(); // Produces: SELECT title, content, date FROM mytable |
| 185 | |
| 186 | .. note:: As shown earlier, the FROM portion of your query can be specified |
| 187 | in the $this->db->get() function, so use whichever method you prefer. |
| 188 | |
| 189 | $this->db->join() |
| 190 | ================= |
| 191 | |
| 192 | Permits you to write the JOIN portion of your query:: |
| 193 | |
| 194 | $this->db->select('*'); |
| 195 | $this->db->from('blogs'); |
| 196 | $this->db->join('comments', 'comments.id = blogs.id'); |
| 197 | $query = $this->db->get(); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 198 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 199 | // Produces: |
kenjis | c35d2c9 | 2011-10-26 17:09:17 +0900 | [diff] [blame] | 200 | // SELECT * FROM blogs JOIN comments ON comments.id = blogs.id |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 201 | |
| 202 | Multiple function calls can be made if you need several joins in one |
| 203 | query. |
| 204 | |
| 205 | If you need a specific type of JOIN you can specify it via the third |
| 206 | parameter of the function. Options are: left, right, outer, inner, left |
| 207 | outer, and right outer. |
| 208 | |
| 209 | :: |
| 210 | |
| 211 | $this->db->join('comments', 'comments.id = blogs.id', 'left'); |
| 212 | // Produces: LEFT JOIN comments ON comments.id = blogs.id |
| 213 | |
| 214 | $this->db->where() |
| 215 | ================== |
| 216 | |
| 217 | This function enables you to set **WHERE** clauses using one of four |
| 218 | methods: |
| 219 | |
| 220 | .. note:: All values passed to this function are escaped automatically, |
| 221 | producing safer queries. |
| 222 | |
| 223 | #. **Simple key/value method:** |
| 224 | |
| 225 | :: |
| 226 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 227 | $this->db->where('name', $name); // Produces: WHERE name = 'Joe' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 228 | |
| 229 | Notice that the equal sign is added for you. |
| 230 | |
| 231 | If you use multiple function calls they will be chained together with |
| 232 | AND between them: |
| 233 | |
| 234 | :: |
| 235 | |
| 236 | $this->db->where('name', $name); |
| 237 | $this->db->where('title', $title); |
| 238 | $this->db->where('status', $status); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 239 | // WHERE name = 'Joe' AND title = 'boss' AND status = 'active' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 240 | |
| 241 | #. **Custom key/value method:** |
| 242 | You can include an operator in the first parameter in order to |
| 243 | control the comparison: |
| 244 | |
| 245 | :: |
| 246 | |
| 247 | $this->db->where('name !=', $name); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 248 | $this->db->where('id <', $id); // Produces: WHERE name != 'Joe' AND id < 45 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 249 | |
| 250 | #. **Associative array method:** |
| 251 | |
| 252 | :: |
| 253 | |
| 254 | $array = array('name' => $name, 'title' => $title, 'status' => $status); |
| 255 | $this->db->where($array); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 256 | // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 257 | |
| 258 | You can include your own operators using this method as well: |
| 259 | |
| 260 | :: |
| 261 | |
| 262 | $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); |
| 263 | $this->db->where($array); |
| 264 | |
| 265 | #. **Custom string:** |
| 266 | You can write your own clauses manually:: |
| 267 | |
| 268 | $where = "name='Joe' AND status='boss' OR status='active'"; |
| 269 | $this->db->where($where); |
| 270 | |
| 271 | |
| 272 | $this->db->where() accepts an optional third parameter. If you set it to |
| 273 | FALSE, CodeIgniter will not try to protect your field or table names |
| 274 | with backticks. |
| 275 | |
| 276 | :: |
| 277 | |
| 278 | $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE); |
| 279 | |
| 280 | |
| 281 | $this->db->or_where() |
| 282 | ===================== |
| 283 | |
| 284 | This function is identical to the one above, except that multiple |
| 285 | instances are joined by OR:: |
| 286 | |
| 287 | $this->db->where('name !=', $name); |
| 288 | $this->db->or_where('id >', $id); // Produces: WHERE name != 'Joe' OR id > 50 |
| 289 | |
| 290 | .. note:: or_where() was formerly known as orwhere(), which has been |
| 291 | removed. |
| 292 | |
| 293 | $this->db->where_in() |
| 294 | ===================== |
| 295 | |
| 296 | Generates a WHERE field IN ('item', 'item') SQL query joined with AND if |
| 297 | appropriate |
| 298 | |
| 299 | :: |
| 300 | |
| 301 | $names = array('Frank', 'Todd', 'James'); |
| 302 | $this->db->where_in('username', $names); |
| 303 | // Produces: WHERE username IN ('Frank', 'Todd', 'James') |
| 304 | |
| 305 | |
| 306 | $this->db->or_where_in() |
| 307 | ======================== |
| 308 | |
| 309 | Generates a WHERE field IN ('item', 'item') SQL query joined with OR if |
| 310 | appropriate |
| 311 | |
| 312 | :: |
| 313 | |
| 314 | $names = array('Frank', 'Todd', 'James'); |
| 315 | $this->db->or_where_in('username', $names); |
| 316 | // Produces: OR username IN ('Frank', 'Todd', 'James') |
| 317 | |
| 318 | |
| 319 | $this->db->where_not_in() |
| 320 | ========================= |
| 321 | |
| 322 | Generates a WHERE field NOT IN ('item', 'item') SQL query joined with |
| 323 | AND if appropriate |
| 324 | |
| 325 | :: |
| 326 | |
| 327 | $names = array('Frank', 'Todd', 'James'); |
| 328 | $this->db->where_not_in('username', $names); |
| 329 | // Produces: WHERE username NOT IN ('Frank', 'Todd', 'James') |
| 330 | |
| 331 | |
| 332 | $this->db->or_where_not_in() |
| 333 | ============================ |
| 334 | |
| 335 | Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR |
| 336 | if appropriate |
| 337 | |
| 338 | :: |
| 339 | |
| 340 | $names = array('Frank', 'Todd', 'James'); |
| 341 | $this->db->or_where_not_in('username', $names); |
| 342 | // Produces: OR username NOT IN ('Frank', 'Todd', 'James') |
| 343 | |
| 344 | |
| 345 | $this->db->like() |
| 346 | ================= |
| 347 | |
| 348 | This function enables you to generate **LIKE** clauses, useful for doing |
| 349 | searches. |
| 350 | |
| 351 | .. note:: All values passed to this function are escaped automatically. |
| 352 | |
| 353 | #. **Simple key/value method:** |
| 354 | |
| 355 | :: |
| 356 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 357 | $this->db->like('title', 'match'); // Produces: WHERE title LIKE '%match%' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 358 | |
| 359 | If you use multiple function calls they will be chained together with |
| 360 | AND between them:: |
| 361 | |
| 362 | $this->db->like('title', 'match'); |
| 363 | $this->db->like('body', 'match'); |
| 364 | // WHERE title LIKE '%match%' AND body LIKE '%match% |
| 365 | |
| 366 | If you want to control where the wildcard (%) is placed, you can use |
| 367 | an optional third argument. Your options are 'before', 'after' and |
| 368 | 'both' (which is the default). |
| 369 | |
| 370 | :: |
| 371 | |
| 372 | $this->db->like('title', 'match', 'before'); // Produces: WHERE title LIKE '%match' |
| 373 | $this->db->like('title', 'match', 'after'); // Produces: WHERE title LIKE 'match%' |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 374 | $this->db->like('title', 'match', 'both'); // Produces: WHERE title LIKE '%match%' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 375 | |
| 376 | #. **Associative array method:** |
| 377 | |
| 378 | :: |
| 379 | |
| 380 | $array = array('title' => $match, 'page1' => $match, 'page2' => $match); |
| 381 | $this->db->like($array); |
| 382 | // WHERE title LIKE '%match%' AND page1 LIKE '%match%' AND page2 LIKE '%match%' |
| 383 | |
| 384 | |
| 385 | $this->db->or_like() |
| 386 | ==================== |
| 387 | |
| 388 | This function is identical to the one above, except that multiple |
| 389 | instances are joined by OR:: |
| 390 | |
| 391 | $this->db->like('title', 'match'); $this->db->or_like('body', $match); |
| 392 | // WHERE title LIKE '%match%' OR body LIKE '%match%' |
| 393 | |
| 394 | .. note:: or_like() was formerly known as orlike(), which has been removed. |
| 395 | |
| 396 | $this->db->not_like() |
| 397 | ===================== |
| 398 | |
| 399 | This function is identical to **like()**, except that it generates NOT |
| 400 | LIKE statements:: |
| 401 | |
| 402 | $this->db->not_like('title', 'match'); // WHERE title NOT LIKE '%match% |
| 403 | |
| 404 | $this->db->or_not_like() |
| 405 | ======================== |
| 406 | |
| 407 | This function is identical to **not_like()**, except that multiple |
| 408 | instances are joined by OR:: |
| 409 | |
| 410 | $this->db->like('title', 'match'); |
| 411 | $this->db->or_not_like('body', 'match'); |
| 412 | // WHERE title LIKE '%match% OR body NOT LIKE '%match%' |
| 413 | |
| 414 | $this->db->group_by() |
| 415 | ===================== |
| 416 | |
| 417 | Permits you to write the GROUP BY portion of your query:: |
| 418 | |
| 419 | $this->db->group_by("title"); // Produces: GROUP BY title |
| 420 | |
| 421 | You can also pass an array of multiple values as well:: |
| 422 | |
| 423 | $this->db->group_by(array("title", "date")); // Produces: GROUP BY title, date |
| 424 | |
| 425 | .. note:: group_by() was formerly known as groupby(), which has been |
| 426 | removed. |
| 427 | |
| 428 | $this->db->distinct() |
| 429 | ===================== |
| 430 | |
| 431 | Adds the "DISTINCT" keyword to a query |
| 432 | |
| 433 | :: |
| 434 | |
| 435 | $this->db->distinct(); |
| 436 | $this->db->get('table'); // Produces: SELECT DISTINCT * FROM table |
| 437 | |
| 438 | |
| 439 | $this->db->having() |
| 440 | =================== |
| 441 | |
| 442 | Permits you to write the HAVING portion of your query. There are 2 |
| 443 | possible syntaxes, 1 argument or 2:: |
| 444 | |
| 445 | $this->db->having('user_id = 45'); // Produces: HAVING user_id = 45 |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 446 | $this->db->having('user_id', 45); // Produces: HAVING user_id = 45 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 447 | |
| 448 | You can also pass an array of multiple values as well:: |
| 449 | |
| 450 | $this->db->having(array('title =' => 'My Title', 'id <' => $id)); |
| 451 | // Produces: HAVING title = 'My Title', id < 45 |
| 452 | |
| 453 | |
| 454 | If you are using a database that CodeIgniter escapes queries for, you |
| 455 | can prevent escaping content by passing an optional third argument, and |
| 456 | setting it to FALSE. |
| 457 | |
| 458 | :: |
| 459 | |
| 460 | $this->db->having('user_id', 45); // Produces: HAVING `user_id` = 45 in some databases such as MySQL |
| 461 | $this->db->having('user_id', 45, FALSE); // Produces: HAVING user_id = 45 |
| 462 | |
| 463 | |
| 464 | $this->db->or_having() |
| 465 | ====================== |
| 466 | |
| 467 | Identical to having(), only separates multiple clauses with "OR". |
| 468 | |
| 469 | $this->db->order_by() |
| 470 | ===================== |
| 471 | |
| 472 | Lets you set an ORDER BY clause. The first parameter contains the name |
| 473 | of the column you would like to order by. The second parameter lets you |
| 474 | set the direction of the result. Options are asc or desc, or random. |
| 475 | |
| 476 | :: |
| 477 | |
| 478 | $this->db->order_by("title", "desc"); // Produces: ORDER BY title DESC |
| 479 | |
| 480 | You can also pass your own string in the first parameter:: |
| 481 | |
| 482 | $this->db->order_by('title desc, name asc'); // Produces: ORDER BY title DESC, name ASC |
| 483 | |
| 484 | Or multiple function calls can be made if you need multiple fields. |
| 485 | |
| 486 | :: |
| 487 | |
| 488 | $this->db->order_by("title", "desc"); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 489 | $this->db->order_by("name", "asc"); // Produces: ORDER BY title DESC, name ASC |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 490 | |
| 491 | |
| 492 | .. note:: order_by() was formerly known as orderby(), which has been |
| 493 | removed. |
| 494 | |
| 495 | .. note:: random ordering is not currently supported in Oracle or MSSQL |
| 496 | drivers. These will default to 'ASC'. |
| 497 | |
| 498 | $this->db->limit() |
| 499 | ================== |
| 500 | |
| 501 | Lets you limit the number of rows you would like returned by the query:: |
| 502 | |
| 503 | $this->db->limit(10); // Produces: LIMIT 10 |
| 504 | |
| 505 | The second parameter lets you set a result offset. |
| 506 | |
| 507 | :: |
| 508 | |
| 509 | $this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax) |
| 510 | |
| 511 | $this->db->count_all_results() |
| 512 | ============================== |
| 513 | |
| 514 | Permits you to determine the number of rows in a particular Active |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 515 | Record query. Queries will accept Query Builder restrictors such as |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 516 | where(), or_where(), like(), or_like(), etc. Example:: |
| 517 | |
| 518 | echo $this->db->count_all_results('my_table'); // Produces an integer, like 25 |
| 519 | $this->db->like('title', 'match'); |
| 520 | $this->db->from('my_table'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 521 | echo $this->db->count_all_results(); // Produces an integer, like 17 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 522 | |
| 523 | $this->db->count_all() |
| 524 | ====================== |
| 525 | |
| 526 | Permits you to determine the number of rows in a particular table. |
| 527 | Submit the table name in the first parameter. Example:: |
| 528 | |
| 529 | echo $this->db->count_all('my_table'); // Produces an integer, like 25 |
| 530 | |
| 531 | ************** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 532 | Query grouping |
| 533 | ************** |
| 534 | |
| 535 | Query grouping allows you to create groups of WHERE clauses by enclosing them in parentheses. This will allow |
Timothy Warren | e464b39 | 2012-03-13 14:09:31 -0400 | [diff] [blame] | 536 | you to create queries with complex WHERE clauses. Nested groups are supported. Example:: |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 537 | |
| 538 | $this->db->select('*')->from('my_table') |
| 539 | ->group_start() |
| 540 | ->where('a', 'a') |
| 541 | ->or_group_start() |
| 542 | ->where('b', 'b') |
| 543 | ->where('c', 'c') |
| 544 | ->group_end() |
| 545 | ->group_end() |
| 546 | ->where('d', 'd') |
| 547 | ->get(); |
| 548 | |
| 549 | // Generates: |
| 550 | // SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd' |
| 551 | |
| 552 | .. note:: groups need to be balanced, make sure every group_start() is matched by a group_end(). |
| 553 | |
| 554 | $this->db->group_start() |
| 555 | ======================== |
| 556 | |
| 557 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query. |
| 558 | |
| 559 | $this->db->or_group_start() |
| 560 | =========================== |
| 561 | |
| 562 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR'. |
| 563 | |
| 564 | $this->db->not_group_start() |
| 565 | ============================ |
| 566 | |
| 567 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'NOT'. |
| 568 | |
| 569 | $this->db->or_not_group_start() |
| 570 | =============================== |
| 571 | |
| 572 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR NOT'. |
| 573 | |
| 574 | $this->db->group_end() |
| 575 | ====================== |
| 576 | |
| 577 | Ends the current group by adding an closing parenthesis to the WHERE clause of the query. |
| 578 | |
| 579 | ************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 580 | Inserting Data |
| 581 | ************** |
| 582 | |
| 583 | $this->db->insert() |
| 584 | =================== |
| 585 | |
| 586 | Generates an insert string based on the data you supply, and runs the |
| 587 | query. You can either pass an **array** or an **object** to the |
| 588 | function. Here is an example using an array:: |
| 589 | |
| 590 | $data = array( |
| 591 | 'title' => 'My title', |
| 592 | 'name' => 'My Name', |
| 593 | 'date' => 'My date' |
| 594 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 595 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 596 | $this->db->insert('mytable', $data); |
| 597 | // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') |
| 598 | |
| 599 | The first parameter will contain the table name, the second is an |
| 600 | associative array of values. |
| 601 | |
| 602 | Here is an example using an object:: |
| 603 | |
| 604 | /* |
| 605 | class Myclass { |
vlakoff | ff3f7de | 2012-06-16 14:21:32 +0200 | [diff] [blame] | 606 | public $title = 'My Title'; |
| 607 | public $content = 'My Content'; |
| 608 | public $date = 'My Date'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 609 | } |
| 610 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 611 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 612 | $object = new Myclass; |
| 613 | $this->db->insert('mytable', $object); |
| 614 | // Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date') |
| 615 | |
| 616 | The first parameter will contain the table name, the second is an |
| 617 | object. |
| 618 | |
| 619 | .. note:: All values are escaped automatically producing safer queries. |
| 620 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 621 | $this->db->get_compiled_insert() |
| 622 | ================================ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 623 | Compiles the insertion query just like `$this->db->insert()`_ but does not |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 624 | *run* the query. This method simply returns the SQL query as a string. |
| 625 | |
| 626 | Example:: |
| 627 | |
| 628 | $data = array( |
| 629 | 'title' => 'My title', |
| 630 | 'name' => 'My Name', |
| 631 | 'date' => 'My date' |
| 632 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 633 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 634 | $sql = $this->db->set($data)->get_compiled_insert('mytable'); |
| 635 | echo $sql; |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 636 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 637 | // Produces string: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') |
| 638 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 639 | The second parameter enables you to set whether or not the query builder query |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 640 | will be reset (by default it will be--just like `$this->db->insert()`_):: |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 641 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 642 | echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 643 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 644 | // Produces string: INSERT INTO mytable (title) VALUES ('My Title') |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 645 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 646 | echo $this->db->set('content', 'My Content')->get_compiled_insert(); |
| 647 | |
| 648 | // Produces string: INSERT INTO mytable (title, content) VALUES ('My Title', 'My Content') |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 649 | |
| 650 | The key thing to notice in the above example is that the second query did not |
| 651 | utlize `$this->db->from()`_ nor did it pass a table name into the first |
| 652 | parameter. The reason this worked is because the query has not been executed |
| 653 | using `$this->db->insert()`_ which resets values or reset directly using |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 654 | `$this->db->reset_query()`_. |
| 655 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 656 | $this->db->insert_batch() |
| 657 | ========================= |
| 658 | |
| 659 | Generates an insert string based on the data you supply, and runs the |
| 660 | query. You can either pass an **array** or an **object** to the |
| 661 | function. Here is an example using an array:: |
| 662 | |
| 663 | $data = array( |
| 664 | array( |
| 665 | 'title' => 'My title', |
| 666 | 'name' => 'My Name', |
| 667 | 'date' => 'My date' |
| 668 | ), |
| 669 | array( |
| 670 | 'title' => 'Another title', |
| 671 | 'name' => 'Another Name', |
| 672 | 'date' => 'Another date' |
| 673 | ) |
| 674 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 675 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 676 | $this->db->insert_batch('mytable', $data); |
| 677 | // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date') |
| 678 | |
| 679 | The first parameter will contain the table name, the second is an |
| 680 | associative array of values. |
| 681 | |
| 682 | .. note:: All values are escaped automatically producing safer queries. |
| 683 | |
| 684 | $this->db->set() |
| 685 | ================ |
| 686 | |
| 687 | This function enables you to set values for inserts or updates. |
| 688 | |
| 689 | **It can be used instead of passing a data array directly to the insert |
| 690 | or update functions:** |
| 691 | |
| 692 | :: |
| 693 | |
| 694 | $this->db->set('name', $name); |
| 695 | $this->db->insert('mytable'); // Produces: INSERT INTO mytable (name) VALUES ('{$name}') |
| 696 | |
| 697 | If you use multiple function called they will be assembled properly |
| 698 | based on whether you are doing an insert or an update:: |
| 699 | |
| 700 | $this->db->set('name', $name); |
| 701 | $this->db->set('title', $title); |
| 702 | $this->db->set('status', $status); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 703 | $this->db->insert('mytable'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 704 | |
| 705 | **set()** will also accept an optional third parameter ($escape), that |
| 706 | will prevent data from being escaped if set to FALSE. To illustrate the |
| 707 | difference, here is set() used both with and without the escape |
| 708 | parameter. |
| 709 | |
| 710 | :: |
| 711 | |
| 712 | $this->db->set('field', 'field+1', FALSE); |
| 713 | $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES (field+1) |
| 714 | $this->db->set('field', 'field+1'); |
| 715 | $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES ('field+1') |
| 716 | |
| 717 | |
| 718 | You can also pass an associative array to this function:: |
| 719 | |
| 720 | $array = array( |
| 721 | 'name' => $name, |
| 722 | 'title' => $title, |
| 723 | 'status' => $status |
| 724 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 725 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 726 | $this->db->set($array); |
| 727 | $this->db->insert('mytable'); |
| 728 | |
| 729 | Or an object:: |
| 730 | |
| 731 | /* |
| 732 | class Myclass { |
vlakoff | ff3f7de | 2012-06-16 14:21:32 +0200 | [diff] [blame] | 733 | public $title = 'My Title'; |
| 734 | public $content = 'My Content'; |
| 735 | public $date = 'My Date'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 736 | } |
| 737 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 738 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 739 | $object = new Myclass; |
| 740 | $this->db->set($object); |
| 741 | $this->db->insert('mytable'); |
| 742 | |
| 743 | |
| 744 | ************* |
| 745 | Updating Data |
| 746 | ************* |
| 747 | |
| 748 | $this->db->update() |
| 749 | =================== |
| 750 | |
| 751 | Generates an update string and runs the query based on the data you |
| 752 | supply. You can pass an **array** or an **object** to the function. Here |
| 753 | is an example using an array:: |
| 754 | |
| 755 | $data = array( |
| 756 | 'title' => $title, |
| 757 | 'name' => $name, |
| 758 | 'date' => $date |
| 759 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 760 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 761 | $this->db->where('id', $id); |
| 762 | $this->db->update('mytable', $data); |
| 763 | // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id |
| 764 | |
| 765 | Or you can supply an object:: |
| 766 | |
| 767 | /* |
| 768 | class Myclass { |
vlakoff | ff3f7de | 2012-06-16 14:21:32 +0200 | [diff] [blame] | 769 | public $title = 'My Title'; |
| 770 | public $content = 'My Content'; |
| 771 | public $date = 'My Date'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 772 | } |
| 773 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 774 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 775 | $object = new Myclass; |
| 776 | $this->db->where('id', $id); |
| 777 | $this->db->update('mytable', $object); |
| 778 | // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id |
| 779 | |
| 780 | .. note:: All values are escaped automatically producing safer queries. |
| 781 | |
| 782 | You'll notice the use of the $this->db->where() function, enabling you |
| 783 | to set the WHERE clause. You can optionally pass this information |
| 784 | directly into the update function as a string:: |
| 785 | |
| 786 | $this->db->update('mytable', $data, "id = 4"); |
| 787 | |
| 788 | Or as an array:: |
| 789 | |
| 790 | $this->db->update('mytable', $data, array('id' => $id)); |
| 791 | |
| 792 | You may also use the $this->db->set() function described above when |
| 793 | performing updates. |
| 794 | |
| 795 | $this->db->update_batch() |
| 796 | ========================= |
| 797 | |
| 798 | Generates an update string based on the data you supply, and runs the query. |
| 799 | You can either pass an **array** or an **object** to the function. |
| 800 | Here is an example using an array:: |
| 801 | |
| 802 | $data = array( |
| 803 | array( |
| 804 | 'title' => 'My title' , |
| 805 | 'name' => 'My Name 2' , |
| 806 | 'date' => 'My date 2' |
| 807 | ), |
| 808 | array( |
| 809 | 'title' => 'Another title' , |
| 810 | 'name' => 'Another Name 2' , |
| 811 | 'date' => 'Another date 2' |
| 812 | ) |
| 813 | ); |
| 814 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 815 | $this->db->update_batch('mytable', $data, 'title'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 816 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 817 | // Produces: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 818 | // UPDATE `mytable` SET `name` = CASE |
| 819 | // WHEN `title` = 'My title' THEN 'My Name 2' |
| 820 | // WHEN `title` = 'Another title' THEN 'Another Name 2' |
| 821 | // ELSE `name` END, |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 822 | // `date` = CASE |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 823 | // WHEN `title` = 'My title' THEN 'My date 2' |
| 824 | // WHEN `title` = 'Another title' THEN 'Another date 2' |
| 825 | // ELSE `date` END |
| 826 | // WHERE `title` IN ('My title','Another title') |
| 827 | |
| 828 | The first parameter will contain the table name, the second is an associative |
| 829 | array of values, the third parameter is the where key. |
| 830 | |
| 831 | .. note:: All values are escaped automatically producing safer queries. |
| 832 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 833 | $this->db->get_compiled_update() |
| 834 | ================================ |
| 835 | |
| 836 | This works exactly the same way as ``$this->db->get_compiled_insert()`` except |
| 837 | that it produces an UPDATE SQL string instead of an INSERT SQL string. |
| 838 | |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame] | 839 | For more information view documentation for `$this->db->get_compiled_insert()`_. |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 840 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 841 | |
| 842 | ************* |
| 843 | Deleting Data |
| 844 | ************* |
| 845 | |
| 846 | $this->db->delete() |
| 847 | =================== |
| 848 | |
| 849 | Generates a delete SQL string and runs the query. |
| 850 | |
| 851 | :: |
| 852 | |
| 853 | $this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id |
| 854 | |
| 855 | The first parameter is the table name, the second is the where clause. |
| 856 | You can also use the where() or or_where() functions instead of passing |
| 857 | the data to the second parameter of the function:: |
| 858 | |
| 859 | $this->db->where('id', $id); |
| 860 | $this->db->delete('mytable'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 861 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 862 | // Produces: |
| 863 | // DELETE FROM mytable |
| 864 | // WHERE id = $id |
| 865 | |
| 866 | |
| 867 | An array of table names can be passed into delete() if you would like to |
| 868 | delete data from more than 1 table. |
| 869 | |
| 870 | :: |
| 871 | |
| 872 | $tables = array('table1', 'table2', 'table3'); |
| 873 | $this->db->where('id', '5'); |
| 874 | $this->db->delete($tables); |
| 875 | |
| 876 | |
| 877 | If you want to delete all data from a table, you can use the truncate() |
| 878 | function, or empty_table(). |
| 879 | |
| 880 | $this->db->empty_table() |
| 881 | ======================== |
| 882 | |
| 883 | Generates a delete SQL string and runs the |
| 884 | query.:: |
| 885 | |
kenjis | c35d2c9 | 2011-10-26 17:09:17 +0900 | [diff] [blame] | 886 | $this->db->empty_table('mytable'); // Produces: DELETE FROM mytable |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 887 | |
| 888 | |
| 889 | $this->db->truncate() |
| 890 | ===================== |
| 891 | |
| 892 | Generates a truncate SQL string and runs the query. |
| 893 | |
| 894 | :: |
| 895 | |
| 896 | $this->db->from('mytable'); |
kenjis | c35d2c9 | 2011-10-26 17:09:17 +0900 | [diff] [blame] | 897 | $this->db->truncate(); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 898 | |
| 899 | // or |
| 900 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 901 | $this->db->truncate('mytable'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 902 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 903 | // Produce: |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 904 | // TRUNCATE mytable |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 905 | |
| 906 | .. note:: If the TRUNCATE command isn't available, truncate() will |
| 907 | execute as "DELETE FROM table". |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 908 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 909 | $this->db->get_compiled_delete() |
| 910 | ================================ |
| 911 | This works exactly the same way as ``$this->db->get_compiled_insert()`` except |
| 912 | that it produces a DELETE SQL string instead of an INSERT SQL string. |
| 913 | |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame] | 914 | For more information view documentation for `$this->db->get_compiled_insert()`_. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 915 | |
| 916 | *************** |
| 917 | Method Chaining |
| 918 | *************** |
| 919 | |
| 920 | Method chaining allows you to simplify your syntax by connecting |
| 921 | multiple functions. Consider this example:: |
| 922 | |
| 923 | $query = $this->db->select('title') |
Timothy Warren | e464b39 | 2012-03-13 14:09:31 -0400 | [diff] [blame] | 924 | ->where('id', $id) |
| 925 | ->limit(10, 20) |
| 926 | ->get('mytable'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 927 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 928 | .. _ar-caching: |
| 929 | |
| 930 | ********************* |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 931 | Query Builder Caching |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 932 | ********************* |
| 933 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 934 | While not "true" caching, Query Builder enables you to save (or "cache") |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 935 | certain parts of your queries for reuse at a later point in your |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 936 | script's execution. Normally, when an Query Builder call is completed, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 937 | all stored information is reset for the next call. With caching, you can |
| 938 | prevent this reset, and reuse information easily. |
| 939 | |
| 940 | Cached calls are cumulative. If you make 2 cached select() calls, and |
| 941 | then 2 uncached select() calls, this will result in 4 select() calls. |
| 942 | There are three Caching functions available: |
| 943 | |
| 944 | $this->db->start_cache() |
| 945 | ======================== |
| 946 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 947 | This function must be called to begin caching. All Query Builder queries |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 948 | of the correct type (see below for supported queries) are stored for |
| 949 | later use. |
| 950 | |
| 951 | $this->db->stop_cache() |
| 952 | ======================= |
| 953 | |
| 954 | This function can be called to stop caching. |
| 955 | |
| 956 | $this->db->flush_cache() |
| 957 | ======================== |
| 958 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 959 | This function deletes all items from the Query Builder cache. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 960 | |
| 961 | Here's a usage example:: |
| 962 | |
| 963 | $this->db->start_cache(); |
| 964 | $this->db->select('field1'); |
| 965 | $this->db->stop_cache(); |
| 966 | $this->db->get('tablename'); |
| 967 | //Generates: SELECT `field1` FROM (`tablename`) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 968 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 969 | $this->db->select('field2'); |
| 970 | $this->db->get('tablename'); |
| 971 | //Generates: SELECT `field1`, `field2` FROM (`tablename`) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 972 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 973 | $this->db->flush_cache(); |
| 974 | $this->db->select('field2'); |
| 975 | $this->db->get('tablename'); |
| 976 | //Generates: SELECT `field2` FROM (`tablename`) |
| 977 | |
| 978 | |
| 979 | .. note:: The following statements can be cached: select, from, join, |
| 980 | where, like, group_by, having, order_by, set |
| 981 | |
| 982 | |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame] | 983 | $this->db->reset_query() |
| 984 | ======================== |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 985 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 986 | Resetting Query Builder allows you to start fresh with your query without |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 987 | executing it first using a method like $this->db->get() or $this->db->insert(). |
| 988 | Just like the methods that execute a query, this will *not* reset items you've |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 989 | cached using `Query Builder Caching`_. |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 990 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 991 | This is useful in situations where you are using Query Builder to generate SQL |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 992 | (ex. ``$this->db->get_compiled_select()``) but then choose to, for instance, |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 993 | run the query:: |
| 994 | |
| 995 | // Note that the second parameter of the get_compiled_select method is FALSE |
| 996 | $sql = $this->db->select(array('field1','field2')) |
| 997 | ->where('field3',5) |
| 998 | ->get_compiled_select('mytable', FALSE); |
| 999 | |
| 1000 | // ... |
| 1001 | // Do something crazy with the SQL code... like add it to a cron script for |
| 1002 | // later execution or something... |
| 1003 | // ... |
| 1004 | |
| 1005 | $data = $this->db->get()->result_array(); |
| 1006 | |
| 1007 | // Would execute and return an array of results of the following query: |
| 1008 | // SELECT field1, field1 from mytable where field3 = 5; |