blob: 3203ff103748da29462b72a94550f7ec0acd50ed [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###################
Jamie Rumbelow7efad202012-02-19 12:37:00 +00002Query Builder Class
Derek Jones8ede1a22011-10-05 13:34:52 -05003###################
4
Jamie Rumbelow7efad202012-02-19 12:37:00 +00005CodeIgniter gives you access to a Query Builder class. This pattern
6allows information to be retrieved, inserted, and updated in your
7database with minimal scripting. In some cases only one or two lines
8of code are necessary to perform a database action.
Derek Jones8ede1a22011-10-05 13:34:52 -05009CodeIgniter does not require that each database table be its own class
10file. It instead provides a more simplified interface.
11
Jamie Rumbelow7efad202012-02-19 12:37:00 +000012Beyond simplicity, a major benefit to using the Query Builder features
Derek Jones8ede1a22011-10-05 13:34:52 -050013is that it allows you to create database independent applications, since
14the query syntax is generated by each database adapter. It also allows
15for safer queries, since the values are escaped automatically by the
16system.
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
James L Parry42a7df62014-11-25 12:06:49 -080022.. contents::
23 :local:
24 :depth: 1
Derek Jones8ede1a22011-10-05 13:34:52 -050025
26**************
27Selecting Data
28**************
29
30The following functions allow you to build SQL **SELECT** statements.
31
32$this->db->get()
James L Parry42a7df62014-11-25 12:06:49 -080033----------------
Derek Jones8ede1a22011-10-05 13:34:52 -050034
35Runs the selection query and returns the result. Can be used by itself
36to retrieve all records from a table::
37
38 $query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable
39
40The second and third parameters enable you to set a limit and offset
41clause::
42
43 $query = $this->db->get('mytable', 10, 20);
James L Parry42a7df62014-11-25 12:06:49 -080044 // Produces: SELECT * FROM mytable LIMIT 20, 10
45 // (in MySQL. Other databases have slightly different syntax)
Derek Jones8ede1a22011-10-05 13:34:52 -050046
47You'll notice that the above function is assigned to a variable named
48$query, which can be used to show the results::
49
50 $query = $this->db->get('mytable');
WanWizard7219c072011-12-28 14:09:05 +010051
Derek Jones8ede1a22011-10-05 13:34:52 -050052 foreach ($query->result() as $row)
53 {
54 echo $row->title;
55 }
56
57Please visit the :doc:`result functions <results>` page for a full
58discussion regarding result generation.
59
James L Parry42a7df62014-11-25 12:06:49 -080060:returns: DB_Result for a successful "read",
61 TRUE for a successful "write", FALSE if an error
Kyle Farris48d8fb62011-10-14 17:59:49 -030062
James L Parry42a7df62014-11-25 12:06:49 -080063$this->db->get_compiled_select()
64--------------------------------
65
66Compiles the selection query just like **$this->db->get()** but does not *run*
Kyle Farris48d8fb62011-10-14 17:59:49 -030067the query. This method simply returns the SQL query as a string.
68
69Example::
70
71 $sql = $this->db->get_compiled_select('mytable');
72 echo $sql;
WanWizard7219c072011-12-28 14:09:05 +010073
Kyle Farris48d8fb62011-10-14 17:59:49 -030074 // Produces string: SELECT * FROM mytable
WanWizard7219c072011-12-28 14:09:05 +010075
Jamie Rumbelow7efad202012-02-19 12:37:00 +000076The second parameter enables you to set whether or not the query builder query
GDmac01e9fb12013-11-09 08:01:52 +010077will be reset (by default it will be reset, just like when using `$this->db->get()`)::
Kyle Farris48d8fb62011-10-14 17:59:49 -030078
79 echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE);
WanWizard7219c072011-12-28 14:09:05 +010080 // Produces string: SELECT * FROM mytable LIMIT 20, 10
Kyle Farris48d8fb62011-10-14 17:59:49 -030081 // (in MySQL. Other databases have slightly different syntax)
WanWizard7219c072011-12-28 14:09:05 +010082
Kyle Farris48d8fb62011-10-14 17:59:49 -030083 echo $this->db->select('title, content, date')->get_compiled_select();
84
GDmac01e9fb12013-11-09 08:01:52 +010085 // Produces string: SELECT title, content, date FROM mytable LIMIT 20, 10
WanWizard7219c072011-12-28 14:09:05 +010086
87The key thing to notice in the above example is that the second query did not
James L Parry42a7df62014-11-25 12:06:49 -080088utilize **$this->db->from()** and did not pass a table name into the first
WanWizard7219c072011-12-28 14:09:05 +010089parameter. The reason for this outcome is because the query has not been
James L Parry42a7df62014-11-25 12:06:49 -080090executed using **$this->db->get()** which resets values or reset directly
91using **$this->db->reset_query()**.
Kyle Farris48d8fb62011-10-14 17:59:49 -030092
James L Parry42a7df62014-11-25 12:06:49 -080093:returns: The SQL select string
Derek Jones84bcc6e2011-10-17 21:24:27 -050094
Derek Jones8ede1a22011-10-05 13:34:52 -050095$this->db->get_where()
James L Parry42a7df62014-11-25 12:06:49 -080096----------------------
Derek Jones8ede1a22011-10-05 13:34:52 -050097
98Identical to the above function except that it permits you to add a
99"where" clause in the second parameter, instead of using the db->where()
100function::
101
102 $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset);
103
104Please read the about the where function below for more information.
105
Greg Akerffd24a42011-12-25 22:27:59 -0600106.. note:: get_where() was formerly known as getwhere(), which has been removed
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
James L Parry42a7df62014-11-25 12:06:49 -0800108:returns: DB_Result for a successful "read",
109 TRUE for a successful "write", FALSE if an error
110
Derek Jones8ede1a22011-10-05 13:34:52 -0500111$this->db->select()
James L Parry42a7df62014-11-25 12:06:49 -0800112-------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500113
114Permits you to write the SELECT portion of your query::
115
116 $this->db->select('title, content, date');
117 $query = $this->db->get('mytable'); // Produces: SELECT title, content, date FROM mytable
118
119
120.. note:: If you are selecting all (\*) from a table you do not need to
121 use this function. When omitted, CodeIgniter assumes you wish to SELECT *
122
123$this->db->select() accepts an optional second parameter. If you set it
124to FALSE, CodeIgniter will not try to protect your field or table names
125with backticks. This is useful if you need a compound select statement.
126
127::
128
WanWizard7219c072011-12-28 14:09:05 +0100129 $this->db->select('(SELECT SUM(payments.amount) FROM payments WHERE payments.invoice_id=4') AS amount_paid', FALSE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500130 $query = $this->db->get('mytable');
131
James L Parry42a7df62014-11-25 12:06:49 -0800132:returns: The query builder object
Derek Jones8ede1a22011-10-05 13:34:52 -0500133
134$this->db->select_max()
James L Parry42a7df62014-11-25 12:06:49 -0800135-----------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500136
137Writes a "SELECT MAX(field)" portion for your query. You can optionally
138include a second parameter to rename the resulting field.
139
140::
141
142 $this->db->select_max('age');
143 $query = $this->db->get('members'); // Produces: SELECT MAX(age) as age FROM members
WanWizard7219c072011-12-28 14:09:05 +0100144
Derek Jones8ede1a22011-10-05 13:34:52 -0500145 $this->db->select_max('age', 'member_age');
146 $query = $this->db->get('members'); // Produces: SELECT MAX(age) as member_age FROM members
147
148
James L Parry42a7df62014-11-25 12:06:49 -0800149**$this->db->select_min()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500150
151Writes a "SELECT MIN(field)" portion for your query. As with
152select_max(), You can optionally include a second parameter to rename
153the resulting field.
154
155::
156
157 $this->db->select_min('age');
158 $query = $this->db->get('members'); // Produces: SELECT MIN(age) as age FROM members
159
160
James L Parry42a7df62014-11-25 12:06:49 -0800161**$this->db->select_avg()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500162
163Writes a "SELECT AVG(field)" portion for your query. As with
164select_max(), You can optionally include a second parameter to rename
165the resulting field.
166
167::
168
169 $this->db->select_avg('age');
170 $query = $this->db->get('members'); // Produces: SELECT AVG(age) as age FROM members
171
172
James L Parry42a7df62014-11-25 12:06:49 -0800173**$this->db->select_sum()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
175Writes a "SELECT SUM(field)" portion for your query. As with
176select_max(), You can optionally include a second parameter to rename
177the resulting field.
178
179::
180
181 $this->db->select_sum('age');
182 $query = $this->db->get('members'); // Produces: SELECT SUM(age) as age FROM members
183
James L Parry42a7df62014-11-25 12:06:49 -0800184:returns: The query builder object
185
Derek Jones8ede1a22011-10-05 13:34:52 -0500186
187$this->db->from()
James L Parry42a7df62014-11-25 12:06:49 -0800188-----------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500189
190Permits you to write the FROM portion of your query::
191
192 $this->db->select('title, content, date');
193 $this->db->from('mytable');
194 $query = $this->db->get(); // Produces: SELECT title, content, date FROM mytable
195
196.. note:: As shown earlier, the FROM portion of your query can be specified
197 in the $this->db->get() function, so use whichever method you prefer.
198
James L Parry42a7df62014-11-25 12:06:49 -0800199:returns: The query builder object
200
Derek Jones8ede1a22011-10-05 13:34:52 -0500201$this->db->join()
James L Parry42a7df62014-11-25 12:06:49 -0800202-----------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500203
204Permits you to write the JOIN portion of your query::
205
206 $this->db->select('*');
207 $this->db->from('blogs');
208 $this->db->join('comments', 'comments.id = blogs.id');
209 $query = $this->db->get();
WanWizard7219c072011-12-28 14:09:05 +0100210
Derek Jones8ede1a22011-10-05 13:34:52 -0500211 // Produces:
kenjisc35d2c92011-10-26 17:09:17 +0900212 // SELECT * FROM blogs JOIN comments ON comments.id = blogs.id
Derek Jones8ede1a22011-10-05 13:34:52 -0500213
214Multiple function calls can be made if you need several joins in one
215query.
216
217If you need a specific type of JOIN you can specify it via the third
218parameter of the function. Options are: left, right, outer, inner, left
219outer, and right outer.
220
221::
222
223 $this->db->join('comments', 'comments.id = blogs.id', 'left');
224 // Produces: LEFT JOIN comments ON comments.id = blogs.id
225
James L Parry42a7df62014-11-25 12:06:49 -0800226:returns: The query builder object
227
228*************************
229Looking for Specific Data
230*************************
231
Derek Jones8ede1a22011-10-05 13:34:52 -0500232$this->db->where()
James L Parry42a7df62014-11-25 12:06:49 -0800233------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500234
235This function enables you to set **WHERE** clauses using one of four
236methods:
237
238.. note:: All values passed to this function are escaped automatically,
239 producing safer queries.
240
241#. **Simple key/value method:**
242
243 ::
244
WanWizard7219c072011-12-28 14:09:05 +0100245 $this->db->where('name', $name); // Produces: WHERE name = 'Joe'
Derek Jones8ede1a22011-10-05 13:34:52 -0500246
247 Notice that the equal sign is added for you.
248
249 If you use multiple function calls they will be chained together with
250 AND between them:
251
252 ::
253
254 $this->db->where('name', $name);
255 $this->db->where('title', $title);
256 $this->db->where('status', $status);
WanWizard7219c072011-12-28 14:09:05 +0100257 // WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
Derek Jones8ede1a22011-10-05 13:34:52 -0500258
259#. **Custom key/value method:**
260 You can include an operator in the first parameter in order to
261 control the comparison:
262
263 ::
264
265 $this->db->where('name !=', $name);
WanWizard7219c072011-12-28 14:09:05 +0100266 $this->db->where('id <', $id); // Produces: WHERE name != 'Joe' AND id < 45
Derek Jones8ede1a22011-10-05 13:34:52 -0500267
268#. **Associative array method:**
269
270 ::
271
272 $array = array('name' => $name, 'title' => $title, 'status' => $status);
273 $this->db->where($array);
WanWizard7219c072011-12-28 14:09:05 +0100274 // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active'
Derek Jones8ede1a22011-10-05 13:34:52 -0500275
276 You can include your own operators using this method as well:
277
278 ::
279
280 $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
281 $this->db->where($array);
282
283#. **Custom string:**
284 You can write your own clauses manually::
285
286 $where = "name='Joe' AND status='boss' OR status='active'";
287 $this->db->where($where);
288
289
290$this->db->where() accepts an optional third parameter. If you set it to
291FALSE, CodeIgniter will not try to protect your field or table names
292with backticks.
293
294::
295
296 $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE);
297
James L Parry42a7df62014-11-25 12:06:49 -0800298**$this->db->or_where()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500299
300This function is identical to the one above, except that multiple
301instances are joined by OR::
302
303 $this->db->where('name !=', $name);
304 $this->db->or_where('id >', $id); // Produces: WHERE name != 'Joe' OR id > 50
305
306.. note:: or_where() was formerly known as orwhere(), which has been
307 removed.
308
James L Parry42a7df62014-11-25 12:06:49 -0800309:returns: The query builder object
310
Derek Jones8ede1a22011-10-05 13:34:52 -0500311$this->db->where_in()
James L Parry42a7df62014-11-25 12:06:49 -0800312---------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500313
314Generates a WHERE field IN ('item', 'item') SQL query joined with AND if
315appropriate
316
317::
318
319 $names = array('Frank', 'Todd', 'James');
320 $this->db->where_in('username', $names);
321 // Produces: WHERE username IN ('Frank', 'Todd', 'James')
322
323
James L Parry42a7df62014-11-25 12:06:49 -0800324**$this->db->or_where_in()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500325
326Generates a WHERE field IN ('item', 'item') SQL query joined with OR if
327appropriate
328
329::
330
331 $names = array('Frank', 'Todd', 'James');
332 $this->db->or_where_in('username', $names);
333 // Produces: OR username IN ('Frank', 'Todd', 'James')
334
James L Parry42a7df62014-11-25 12:06:49 -0800335:returns: The query builder object
Derek Jones8ede1a22011-10-05 13:34:52 -0500336
337$this->db->where_not_in()
James L Parry42a7df62014-11-25 12:06:49 -0800338-------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500339
340Generates a WHERE field NOT IN ('item', 'item') SQL query joined with
341AND if appropriate
342
343::
344
345 $names = array('Frank', 'Todd', 'James');
346 $this->db->where_not_in('username', $names);
347 // Produces: WHERE username NOT IN ('Frank', 'Todd', 'James')
348
349
James L Parry42a7df62014-11-25 12:06:49 -0800350**$this->db->or_where_not_in()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500351
352Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR
353if appropriate
354
355::
356
357 $names = array('Frank', 'Todd', 'James');
358 $this->db->or_where_not_in('username', $names);
359 // Produces: OR username NOT IN ('Frank', 'Todd', 'James')
360
James L Parry42a7df62014-11-25 12:06:49 -0800361:returns: The query builder object
362
363
364************************
365Looking for Similar Data
366************************
Derek Jones8ede1a22011-10-05 13:34:52 -0500367
368$this->db->like()
James L Parry42a7df62014-11-25 12:06:49 -0800369-----------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500370
Andrey Andreev41738232012-11-30 00:13:17 +0200371This method enables you to generate **LIKE** clauses, useful for doing
Derek Jones8ede1a22011-10-05 13:34:52 -0500372searches.
373
Andrey Andreev41738232012-11-30 00:13:17 +0200374.. note:: All values passed to this method are escaped automatically.
Derek Jones8ede1a22011-10-05 13:34:52 -0500375
376#. **Simple key/value method:**
377
378 ::
379
Andrey Andreev41738232012-11-30 00:13:17 +0200380 $this->db->like('title', 'match');
381 // Produces: WHERE `title` LIKE '%match%' ESCAPE '!'
Derek Jones8ede1a22011-10-05 13:34:52 -0500382
Andrey Andreev41738232012-11-30 00:13:17 +0200383 If you use multiple method calls they will be chained together with
Derek Jones8ede1a22011-10-05 13:34:52 -0500384 AND between them::
385
386 $this->db->like('title', 'match');
387 $this->db->like('body', 'match');
Andrey Andreev41738232012-11-30 00:13:17 +0200388 // WHERE `title` LIKE '%match%' ESCAPE '!' AND `body` LIKE '%match% ESCAPE '!'
Derek Jones8ede1a22011-10-05 13:34:52 -0500389
390 If you want to control where the wildcard (%) is placed, you can use
391 an optional third argument. Your options are 'before', 'after' and
392 'both' (which is the default).
393
394 ::
395
Andrey Andreev41738232012-11-30 00:13:17 +0200396 $this->db->like('title', 'match', 'before'); // Produces: WHERE `title` LIKE '%match' ESCAPE '!'
397 $this->db->like('title', 'match', 'after'); // Produces: WHERE `title` LIKE 'match%' ESCAPE '!'
398 $this->db->like('title', 'match', 'both'); // Produces: WHERE `title` LIKE '%match%' ESCAPE '!'
Derek Jones8ede1a22011-10-05 13:34:52 -0500399
400#. **Associative array method:**
401
402 ::
403
404 $array = array('title' => $match, 'page1' => $match, 'page2' => $match);
405 $this->db->like($array);
Andrey Andreev41738232012-11-30 00:13:17 +0200406 // WHERE `title` LIKE '%match%' ESCAPE '!' AND `page1` LIKE '%match%' ESCAPE '!' AND `page2` LIKE '%match%' ESCAPE '!'
Derek Jones8ede1a22011-10-05 13:34:52 -0500407
408
James L Parry42a7df62014-11-25 12:06:49 -0800409**$this->db->or_like()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500410
Andrey Andreev41738232012-11-30 00:13:17 +0200411This method is identical to the one above, except that multiple
Derek Jones8ede1a22011-10-05 13:34:52 -0500412instances are joined by OR::
413
414 $this->db->like('title', 'match'); $this->db->or_like('body', $match);
Andrey Andreev41738232012-11-30 00:13:17 +0200415 // WHERE `title` LIKE '%match%' ESCAPE '!' OR `body` LIKE '%match%' ESCAPE '!'
Derek Jones8ede1a22011-10-05 13:34:52 -0500416
Andrey Andreev41738232012-11-30 00:13:17 +0200417.. note:: ``or_like()`` was formerly known as ``orlike()``, which has been removed.
Derek Jones8ede1a22011-10-05 13:34:52 -0500418
James L Parry42a7df62014-11-25 12:06:49 -0800419**$this->db->not_like()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500420
Andrey Andreev41738232012-11-30 00:13:17 +0200421This method is identical to ``like()``, except that it generates
422NOT LIKE statements::
Derek Jones8ede1a22011-10-05 13:34:52 -0500423
Andrey Andreev41738232012-11-30 00:13:17 +0200424 $this->db->not_like('title', 'match'); // WHERE `title` NOT LIKE '%match% ESCAPE '!'
Derek Jones8ede1a22011-10-05 13:34:52 -0500425
James L Parry42a7df62014-11-25 12:06:49 -0800426**$this->db->or_not_like()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500427
Andrey Andreev41738232012-11-30 00:13:17 +0200428This method is identical to ``not_like()``, except that multiple
Derek Jones8ede1a22011-10-05 13:34:52 -0500429instances are joined by OR::
430
431 $this->db->like('title', 'match');
432 $this->db->or_not_like('body', 'match');
Andrey Andreev41738232012-11-30 00:13:17 +0200433 // WHERE `title` LIKE '%match% OR `body` NOT LIKE '%match%' ESCAPE '!'
Derek Jones8ede1a22011-10-05 13:34:52 -0500434
James L Parry42a7df62014-11-25 12:06:49 -0800435:returns: The query builder object
436
Derek Jones8ede1a22011-10-05 13:34:52 -0500437$this->db->group_by()
James L Parry42a7df62014-11-25 12:06:49 -0800438---------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500439
440Permits you to write the GROUP BY portion of your query::
441
442 $this->db->group_by("title"); // Produces: GROUP BY title
443
444You can also pass an array of multiple values as well::
445
446 $this->db->group_by(array("title", "date")); // Produces: GROUP BY title, date
447
448.. note:: group_by() was formerly known as groupby(), which has been
449 removed.
450
James L Parry42a7df62014-11-25 12:06:49 -0800451:returns: The query builder object
452
Derek Jones8ede1a22011-10-05 13:34:52 -0500453$this->db->distinct()
James L Parry42a7df62014-11-25 12:06:49 -0800454---------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500455
456Adds the "DISTINCT" keyword to a query
457
458::
459
460 $this->db->distinct();
461 $this->db->get('table'); // Produces: SELECT DISTINCT * FROM table
462
James L Parry42a7df62014-11-25 12:06:49 -0800463:returns: The query builder object
Derek Jones8ede1a22011-10-05 13:34:52 -0500464
465$this->db->having()
James L Parry42a7df62014-11-25 12:06:49 -0800466-------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500467
468Permits you to write the HAVING portion of your query. There are 2
469possible syntaxes, 1 argument or 2::
470
471 $this->db->having('user_id = 45'); // Produces: HAVING user_id = 45
WanWizard7219c072011-12-28 14:09:05 +0100472 $this->db->having('user_id', 45); // Produces: HAVING user_id = 45
Derek Jones8ede1a22011-10-05 13:34:52 -0500473
474You can also pass an array of multiple values as well::
475
476 $this->db->having(array('title =' => 'My Title', 'id <' => $id));
477 // Produces: HAVING title = 'My Title', id < 45
478
479
480If you are using a database that CodeIgniter escapes queries for, you
481can prevent escaping content by passing an optional third argument, and
482setting it to FALSE.
483
484::
485
486 $this->db->having('user_id', 45); // Produces: HAVING `user_id` = 45 in some databases such as MySQL
487 $this->db->having('user_id', 45, FALSE); // Produces: HAVING user_id = 45
488
489
James L Parry42a7df62014-11-25 12:06:49 -0800490**$this->db->or_having()**
Derek Jones8ede1a22011-10-05 13:34:52 -0500491
492Identical to having(), only separates multiple clauses with "OR".
493
James L Parry42a7df62014-11-25 12:06:49 -0800494:returns: The query builder object
495
496****************
497Ordering results
498****************
499
Derek Jones8ede1a22011-10-05 13:34:52 -0500500$this->db->order_by()
James L Parry42a7df62014-11-25 12:06:49 -0800501---------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500502
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200503Lets you set an ORDER BY clause.
504
505The first parameter contains the name of the column you would like to order by.
506
507The second parameter lets you set the direction of the result.
508Options are **ASC**, **DESC** AND **RANDOM**.
Derek Jones8ede1a22011-10-05 13:34:52 -0500509
510::
511
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200512 $this->db->order_by('title', 'DESC');
513 // Produces: ORDER BY `title` DESC
Derek Jones8ede1a22011-10-05 13:34:52 -0500514
515You can also pass your own string in the first parameter::
516
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200517 $this->db->order_by('title DESC, name ASC');
518 // Produces: ORDER BY `title` DESC, `name` ASC
Derek Jones8ede1a22011-10-05 13:34:52 -0500519
520Or multiple function calls can be made if you need multiple fields.
521
522::
523
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200524 $this->db->order_by('title', 'DESC');
525 $this->db->order_by('name', 'ASC');
526 // Produces: ORDER BY `title` DESC, `name` ASC
Derek Jones8ede1a22011-10-05 13:34:52 -0500527
Andrey Andreev98e46cf2012-11-13 03:01:42 +0200528If you choose the **RANDOM** direction option, then the first parameters will
529be ignored, unless you specify a numeric seed value.
530
531::
532
533 $this->db->order_by('title', 'RANDOM');
534 // Produces: ORDER BY RAND()
535
536 $this->db->order_by(42, 'RANDOM');
537 // Produces: ORDER BY RAND(42)
Derek Jones8ede1a22011-10-05 13:34:52 -0500538
539.. note:: order_by() was formerly known as orderby(), which has been
540 removed.
541
Andrey Andreev0dfb62f2012-10-30 11:37:15 +0200542.. note:: Random ordering is not currently supported in Oracle and
543 will default to ASC instead.
Derek Jones8ede1a22011-10-05 13:34:52 -0500544
James L Parry42a7df62014-11-25 12:06:49 -0800545:returns: The query builder object
546
547****************************
548Limiting or Counting Results
549****************************
550
Derek Jones8ede1a22011-10-05 13:34:52 -0500551$this->db->limit()
James L Parry42a7df62014-11-25 12:06:49 -0800552------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500553
554Lets you limit the number of rows you would like returned by the query::
555
556 $this->db->limit(10); // Produces: LIMIT 10
557
558The second parameter lets you set a result offset.
559
560::
561
562 $this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax)
563
James L Parry42a7df62014-11-25 12:06:49 -0800564:returns: The query builder object
565
Derek Jones8ede1a22011-10-05 13:34:52 -0500566$this->db->count_all_results()
James L Parry42a7df62014-11-25 12:06:49 -0800567------------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500568
569Permits you to determine the number of rows in a particular Active
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000570Record query. Queries will accept Query Builder restrictors such as
Derek Jones8ede1a22011-10-05 13:34:52 -0500571where(), or_where(), like(), or_like(), etc. Example::
572
573 echo $this->db->count_all_results('my_table'); // Produces an integer, like 25
574 $this->db->like('title', 'match');
575 $this->db->from('my_table');
WanWizard7219c072011-12-28 14:09:05 +0100576 echo $this->db->count_all_results(); // Produces an integer, like 17
Derek Jones8ede1a22011-10-05 13:34:52 -0500577
James L Parry42a7df62014-11-25 12:06:49 -0800578:returns: Count of all the records returned by a query
579
Derek Jones8ede1a22011-10-05 13:34:52 -0500580$this->db->count_all()
James L Parry42a7df62014-11-25 12:06:49 -0800581----------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500582
583Permits you to determine the number of rows in a particular table.
584Submit the table name in the first parameter. Example::
585
586 echo $this->db->count_all('my_table'); // Produces an integer, like 25
587
James L Parry42a7df62014-11-25 12:06:49 -0800588:returns: Count of all the records in the specified table
589
Derek Jones8ede1a22011-10-05 13:34:52 -0500590**************
WanWizard7219c072011-12-28 14:09:05 +0100591Query grouping
592**************
593
594Query grouping allows you to create groups of WHERE clauses by enclosing them in parentheses. This will allow
Timothy Warrene464b392012-03-13 14:09:31 -0400595you to create queries with complex WHERE clauses. Nested groups are supported. Example::
WanWizard7219c072011-12-28 14:09:05 +0100596
597 $this->db->select('*')->from('my_table')
598 ->group_start()
599 ->where('a', 'a')
600 ->or_group_start()
601 ->where('b', 'b')
602 ->where('c', 'c')
603 ->group_end()
604 ->group_end()
605 ->where('d', 'd')
606 ->get();
607
608 // Generates:
609 // SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd'
610
611.. note:: groups need to be balanced, make sure every group_start() is matched by a group_end().
612
James L Parry42a7df62014-11-25 12:06:49 -0800613**$this->db->group_start()**
WanWizard7219c072011-12-28 14:09:05 +0100614
615Starts a new group by adding an opening parenthesis to the WHERE clause of the query.
616
James L Parry42a7df62014-11-25 12:06:49 -0800617**$this->db->or_group_start()**
WanWizard7219c072011-12-28 14:09:05 +0100618
619Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR'.
620
James L Parry42a7df62014-11-25 12:06:49 -0800621**$this->db->not_group_start()**
WanWizard7219c072011-12-28 14:09:05 +0100622
623Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'NOT'.
624
James L Parry42a7df62014-11-25 12:06:49 -0800625**$this->db->or_not_group_start()**
WanWizard7219c072011-12-28 14:09:05 +0100626
627Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR NOT'.
628
James L Parry42a7df62014-11-25 12:06:49 -0800629**$this->db->group_end()**
WanWizard7219c072011-12-28 14:09:05 +0100630
631Ends the current group by adding an closing parenthesis to the WHERE clause of the query.
632
James L Parry42a7df62014-11-25 12:06:49 -0800633:returns: The query builder object
634
WanWizard7219c072011-12-28 14:09:05 +0100635**************
Derek Jones8ede1a22011-10-05 13:34:52 -0500636Inserting Data
637**************
638
639$this->db->insert()
James L Parry42a7df62014-11-25 12:06:49 -0800640-------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500641
642Generates an insert string based on the data you supply, and runs the
643query. You can either pass an **array** or an **object** to the
644function. Here is an example using an array::
645
646 $data = array(
647 'title' => 'My title',
648 'name' => 'My Name',
649 'date' => 'My date'
650 );
WanWizard7219c072011-12-28 14:09:05 +0100651
Derek Jones8ede1a22011-10-05 13:34:52 -0500652 $this->db->insert('mytable', $data);
653 // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
654
655The first parameter will contain the table name, the second is an
656associative array of values.
657
658Here is an example using an object::
659
660 /*
661 class Myclass {
vlakoffff3f7de2012-06-16 14:21:32 +0200662 public $title = 'My Title';
663 public $content = 'My Content';
664 public $date = 'My Date';
Derek Jones8ede1a22011-10-05 13:34:52 -0500665 }
666 */
WanWizard7219c072011-12-28 14:09:05 +0100667
Derek Jones8ede1a22011-10-05 13:34:52 -0500668 $object = new Myclass;
669 $this->db->insert('mytable', $object);
670 // Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')
671
672The first parameter will contain the table name, the second is an
673object.
674
675.. note:: All values are escaped automatically producing safer queries.
676
James L Parry42a7df62014-11-25 12:06:49 -0800677:returns: DB_Query on success, FALSE on failure
678
Kyle Farris48d8fb62011-10-14 17:59:49 -0300679$this->db->get_compiled_insert()
James L Parry42a7df62014-11-25 12:06:49 -0800680--------------------------------
681
WanWizard7219c072011-12-28 14:09:05 +0100682Compiles the insertion query just like `$this->db->insert()`_ but does not
Kyle Farris48d8fb62011-10-14 17:59:49 -0300683*run* the query. This method simply returns the SQL query as a string.
684
685Example::
686
687 $data = array(
688 'title' => 'My title',
689 'name' => 'My Name',
690 'date' => 'My date'
691 );
WanWizard7219c072011-12-28 14:09:05 +0100692
Kyle Farris48d8fb62011-10-14 17:59:49 -0300693 $sql = $this->db->set($data)->get_compiled_insert('mytable');
694 echo $sql;
WanWizard7219c072011-12-28 14:09:05 +0100695
Kyle Farris48d8fb62011-10-14 17:59:49 -0300696 // Produces string: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
697
Jamie Rumbelow7efad202012-02-19 12:37:00 +0000698The second parameter enables you to set whether or not the query builder query
Kyle Farris48d8fb62011-10-14 17:59:49 -0300699will be reset (by default it will be--just like `$this->db->insert()`_)::
WanWizard7219c072011-12-28 14:09:05 +0100700
Kyle Farris48d8fb62011-10-14 17:59:49 -0300701 echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE);
WanWizard7219c072011-12-28 14:09:05 +0100702
Kyle Farris48d8fb62011-10-14 17:59:49 -0300703 // Produces string: INSERT INTO mytable (title) VALUES ('My Title')
WanWizard7219c072011-12-28 14:09:05 +0100704
Kyle Farris48d8fb62011-10-14 17:59:49 -0300705 echo $this->db->set('content', 'My Content')->get_compiled_insert();
706
707 // Produces string: INSERT INTO mytable (title, content) VALUES ('My Title', 'My Content')
WanWizard7219c072011-12-28 14:09:05 +0100708
709The key thing to notice in the above example is that the second query did not
Andrey Andreev6bdfa422013-12-19 15:36:01 +0200710utlize `$this->db->from()` nor did it pass a table name into the first
WanWizard7219c072011-12-28 14:09:05 +0100711parameter. The reason this worked is because the query has not been executed
Andrey Andreev6bdfa422013-12-19 15:36:01 +0200712using `$this->db->insert()` which resets values or reset directly using
713`$this->db->reset_query()`.
714
715.. note:: This method doesn't work for batched inserts.
Kyle Farris48d8fb62011-10-14 17:59:49 -0300716
James L Parry42a7df62014-11-25 12:06:49 -0800717:returns: The SQL insert string
718
Derek Jones8ede1a22011-10-05 13:34:52 -0500719$this->db->insert_batch()
James L Parry42a7df62014-11-25 12:06:49 -0800720-------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500721
722Generates an insert string based on the data you supply, and runs the
723query. You can either pass an **array** or an **object** to the
724function. Here is an example using an array::
725
726 $data = array(
727 array(
728 'title' => 'My title',
729 'name' => 'My Name',
730 'date' => 'My date'
731 ),
732 array(
733 'title' => 'Another title',
734 'name' => 'Another Name',
735 'date' => 'Another date'
736 )
737 );
WanWizard7219c072011-12-28 14:09:05 +0100738
Derek Jones8ede1a22011-10-05 13:34:52 -0500739 $this->db->insert_batch('mytable', $data);
740 // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date')
741
742The first parameter will contain the table name, the second is an
743associative array of values.
744
745.. note:: All values are escaped automatically producing safer queries.
746
James L Parry42a7df62014-11-25 12:06:49 -0800747:returns: Count of the number of records inserted on success, FALSE on failure
748
749*************
750Updating Data
751*************
752
Andrey Andreev04c50f52012-10-24 23:05:25 +0300753$this->db->replace()
James L Parry42a7df62014-11-25 12:06:49 -0800754--------------------
Andrey Andreev04c50f52012-10-24 23:05:25 +0300755
756This method executes a REPLACE statement, which is basically the SQL
757standard for (optional) DELETE + INSERT, using *PRIMARY* and *UNIQUE*
758keys as the determining factor.
759In our case, it will save you from the need to implement complex
760logics with different combinations of ``select()``, ``update()``,
761``delete()`` and ``insert()`` calls.
762
763Example::
764
765 $data = array(
766 'title' => 'My title',
767 'name' => 'My Name',
768 'date' => 'My date'
769 );
770
771 $this->db->replace('table', $data);
772
773 // Executes: REPLACE INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date')
774
775In the above example, if we assume that the *title* field is our primary
776key, then if a row containing 'My title' as the *title* value, that row
777will be deleted with our new row data replacing it.
778
779Usage of the ``set()`` method is also allowed and all fields are
780automatically escaped, just like with ``insert()``.
781
James L Parry42a7df62014-11-25 12:06:49 -0800782:returns: DB_query object on success, FALSE on failure
783
Derek Jones8ede1a22011-10-05 13:34:52 -0500784$this->db->set()
James L Parry42a7df62014-11-25 12:06:49 -0800785----------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500786
787This function enables you to set values for inserts or updates.
788
789**It can be used instead of passing a data array directly to the insert
790or update functions:**
791
792::
793
794 $this->db->set('name', $name);
795 $this->db->insert('mytable'); // Produces: INSERT INTO mytable (name) VALUES ('{$name}')
796
797If you use multiple function called they will be assembled properly
798based on whether you are doing an insert or an update::
799
800 $this->db->set('name', $name);
801 $this->db->set('title', $title);
802 $this->db->set('status', $status);
WanWizard7219c072011-12-28 14:09:05 +0100803 $this->db->insert('mytable');
Derek Jones8ede1a22011-10-05 13:34:52 -0500804
805**set()** will also accept an optional third parameter ($escape), that
806will prevent data from being escaped if set to FALSE. To illustrate the
807difference, here is set() used both with and without the escape
808parameter.
809
810::
811
812 $this->db->set('field', 'field+1', FALSE);
813 $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES (field+1)
814 $this->db->set('field', 'field+1');
815 $this->db->insert('mytable'); // gives INSERT INTO mytable (field) VALUES ('field+1')
816
817
818You can also pass an associative array to this function::
819
820 $array = array(
821 'name' => $name,
822 'title' => $title,
823 'status' => $status
824 );
WanWizard7219c072011-12-28 14:09:05 +0100825
Derek Jones8ede1a22011-10-05 13:34:52 -0500826 $this->db->set($array);
827 $this->db->insert('mytable');
828
829Or an object::
830
831 /*
832 class Myclass {
vlakoffff3f7de2012-06-16 14:21:32 +0200833 public $title = 'My Title';
834 public $content = 'My Content';
835 public $date = 'My Date';
Derek Jones8ede1a22011-10-05 13:34:52 -0500836 }
837 */
WanWizard7219c072011-12-28 14:09:05 +0100838
Derek Jones8ede1a22011-10-05 13:34:52 -0500839 $object = new Myclass;
840 $this->db->set($object);
841 $this->db->insert('mytable');
842
James L Parry42a7df62014-11-25 12:06:49 -0800843:returns: The query builder object
Derek Jones8ede1a22011-10-05 13:34:52 -0500844
845$this->db->update()
James L Parry42a7df62014-11-25 12:06:49 -0800846-------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500847
848Generates an update string and runs the query based on the data you
849supply. You can pass an **array** or an **object** to the function. Here
850is an example using an array::
851
852 $data = array(
853 'title' => $title,
854 'name' => $name,
855 'date' => $date
856 );
WanWizard7219c072011-12-28 14:09:05 +0100857
Derek Jones8ede1a22011-10-05 13:34:52 -0500858 $this->db->where('id', $id);
859 $this->db->update('mytable', $data);
860 // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id
861
862Or you can supply an object::
863
864 /*
865 class Myclass {
vlakoffff3f7de2012-06-16 14:21:32 +0200866 public $title = 'My Title';
867 public $content = 'My Content';
868 public $date = 'My Date';
Derek Jones8ede1a22011-10-05 13:34:52 -0500869 }
870 */
WanWizard7219c072011-12-28 14:09:05 +0100871
Derek Jones8ede1a22011-10-05 13:34:52 -0500872 $object = new Myclass;
873 $this->db->where('id', $id);
874 $this->db->update('mytable', $object);
875 // Produces: // UPDATE mytable // SET title = '{$title}', name = '{$name}', date = '{$date}' // WHERE id = $id
876
877.. note:: All values are escaped automatically producing safer queries.
878
879You'll notice the use of the $this->db->where() function, enabling you
880to set the WHERE clause. You can optionally pass this information
881directly into the update function as a string::
882
883 $this->db->update('mytable', $data, "id = 4");
884
885Or as an array::
886
887 $this->db->update('mytable', $data, array('id' => $id));
888
889You may also use the $this->db->set() function described above when
890performing updates.
891
James L Parry42a7df62014-11-25 12:06:49 -0800892:returns: DB_query object on success, FALSE on failure
Andrey Andreev04c50f52012-10-24 23:05:25 +0300893
Derek Jones8ede1a22011-10-05 13:34:52 -0500894$this->db->update_batch()
James L Parry42a7df62014-11-25 12:06:49 -0800895-------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500896
897Generates an update string based on the data you supply, and runs the query.
898You can either pass an **array** or an **object** to the function.
899Here is an example using an array::
900
901 $data = array(
902 array(
903 'title' => 'My title' ,
904 'name' => 'My Name 2' ,
905 'date' => 'My date 2'
906 ),
907 array(
908 'title' => 'Another title' ,
909 'name' => 'Another Name 2' ,
910 'date' => 'Another date 2'
911 )
912 );
913
WanWizard7219c072011-12-28 14:09:05 +0100914 $this->db->update_batch('mytable', $data, 'title');
Derek Jones8ede1a22011-10-05 13:34:52 -0500915
WanWizard7219c072011-12-28 14:09:05 +0100916 // Produces:
Derek Jones8ede1a22011-10-05 13:34:52 -0500917 // UPDATE `mytable` SET `name` = CASE
918 // WHEN `title` = 'My title' THEN 'My Name 2'
919 // WHEN `title` = 'Another title' THEN 'Another Name 2'
920 // ELSE `name` END,
WanWizard7219c072011-12-28 14:09:05 +0100921 // `date` = CASE
Derek Jones8ede1a22011-10-05 13:34:52 -0500922 // WHEN `title` = 'My title' THEN 'My date 2'
923 // WHEN `title` = 'Another title' THEN 'Another date 2'
924 // ELSE `date` END
925 // WHERE `title` IN ('My title','Another title')
926
927The first parameter will contain the table name, the second is an associative
928array of values, the third parameter is the where key.
929
930.. note:: All values are escaped automatically producing safer queries.
931
Andrey Andreev9f808b02012-10-24 17:38:48 +0300932.. note:: ``affected_rows()`` won't give you proper results with this method,
933 due to the very nature of how it works. Instead, ``update_batch()``
934 returns the number of rows affected.
935
James L Parry42a7df62014-11-25 12:06:49 -0800936:returns: Count of the number of records affected on success, FALSE on failure
937
Kyle Farris48d8fb62011-10-14 17:59:49 -0300938$this->db->get_compiled_update()
James L Parry42a7df62014-11-25 12:06:49 -0800939--------------------------------
Kyle Farris48d8fb62011-10-14 17:59:49 -0300940
941This works exactly the same way as ``$this->db->get_compiled_insert()`` except
942that it produces an UPDATE SQL string instead of an INSERT SQL string.
943
Andrey Andreev6bdfa422013-12-19 15:36:01 +0200944For more information view documentation for `$this->db->get_compiled_insert()`.
Kyle Farris48d8fb62011-10-14 17:59:49 -0300945
Andrey Andreev6bdfa422013-12-19 15:36:01 +0200946.. note:: This method doesn't work for batched updates.
Derek Jones8ede1a22011-10-05 13:34:52 -0500947
James L Parry42a7df62014-11-25 12:06:49 -0800948:returns: The SQL update string
949
Derek Jones8ede1a22011-10-05 13:34:52 -0500950*************
951Deleting Data
952*************
953
954$this->db->delete()
James L Parry42a7df62014-11-25 12:06:49 -0800955-------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500956
957Generates a delete SQL string and runs the query.
958
959::
960
961 $this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id
962
963The first parameter is the table name, the second is the where clause.
964You can also use the where() or or_where() functions instead of passing
965the data to the second parameter of the function::
966
967 $this->db->where('id', $id);
968 $this->db->delete('mytable');
WanWizard7219c072011-12-28 14:09:05 +0100969
Derek Jones8ede1a22011-10-05 13:34:52 -0500970 // Produces:
971 // DELETE FROM mytable
972 // WHERE id = $id
973
974
975An array of table names can be passed into delete() if you would like to
976delete data from more than 1 table.
977
978::
979
980 $tables = array('table1', 'table2', 'table3');
981 $this->db->where('id', '5');
982 $this->db->delete($tables);
983
984
985If you want to delete all data from a table, you can use the truncate()
986function, or empty_table().
987
James L Parry42a7df62014-11-25 12:06:49 -0800988:returns: DB_Query on success, FALSE on failure
989
Derek Jones8ede1a22011-10-05 13:34:52 -0500990$this->db->empty_table()
James L Parry42a7df62014-11-25 12:06:49 -0800991------------------------
Derek Jones8ede1a22011-10-05 13:34:52 -0500992
993Generates a delete SQL string and runs the
994query.::
995
kenjisc35d2c92011-10-26 17:09:17 +0900996 $this->db->empty_table('mytable'); // Produces: DELETE FROM mytable
Derek Jones8ede1a22011-10-05 13:34:52 -0500997
James L Parry42a7df62014-11-25 12:06:49 -0800998:returns: DB_Query on success, FALSE on failure
999
Derek Jones8ede1a22011-10-05 13:34:52 -05001000
1001$this->db->truncate()
James L Parry42a7df62014-11-25 12:06:49 -08001002---------------------
Derek Jones8ede1a22011-10-05 13:34:52 -05001003
1004Generates a truncate SQL string and runs the query.
1005
1006::
1007
1008 $this->db->from('mytable');
kenjisc35d2c92011-10-26 17:09:17 +09001009 $this->db->truncate();
WanWizard7219c072011-12-28 14:09:05 +01001010
1011 // or
1012
Derek Jones8ede1a22011-10-05 13:34:52 -05001013 $this->db->truncate('mytable');
WanWizard7219c072011-12-28 14:09:05 +01001014
Derek Jones8ede1a22011-10-05 13:34:52 -05001015 // Produce:
WanWizard7219c072011-12-28 14:09:05 +01001016 // TRUNCATE mytable
Derek Jones8ede1a22011-10-05 13:34:52 -05001017
1018.. note:: If the TRUNCATE command isn't available, truncate() will
1019 execute as "DELETE FROM table".
WanWizard7219c072011-12-28 14:09:05 +01001020
James L Parry42a7df62014-11-25 12:06:49 -08001021:returns: DB_Query on success, FALSE on failure
1022
Kyle Farris48d8fb62011-10-14 17:59:49 -03001023$this->db->get_compiled_delete()
James L Parry42a7df62014-11-25 12:06:49 -08001024--------------------------------
1025
Kyle Farris48d8fb62011-10-14 17:59:49 -03001026This works exactly the same way as ``$this->db->get_compiled_insert()`` except
1027that it produces a DELETE SQL string instead of an INSERT SQL string.
1028
Greg Akerffd24a42011-12-25 22:27:59 -06001029For more information view documentation for `$this->db->get_compiled_insert()`_.
Derek Jones8ede1a22011-10-05 13:34:52 -05001030
James L Parry42a7df62014-11-25 12:06:49 -08001031:returns: The SQL delete string
1032
1033
1034
Derek Jones8ede1a22011-10-05 13:34:52 -05001035***************
1036Method Chaining
1037***************
1038
1039Method chaining allows you to simplify your syntax by connecting
1040multiple functions. Consider this example::
1041
1042 $query = $this->db->select('title')
Timothy Warrene464b392012-03-13 14:09:31 -04001043 ->where('id', $id)
1044 ->limit(10, 20)
1045 ->get('mytable');
Derek Jones8ede1a22011-10-05 13:34:52 -05001046
Derek Jones8ede1a22011-10-05 13:34:52 -05001047.. _ar-caching:
1048
1049*********************
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001050Query Builder Caching
Derek Jones8ede1a22011-10-05 13:34:52 -05001051*********************
1052
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001053While not "true" caching, Query Builder enables you to save (or "cache")
Derek Jones8ede1a22011-10-05 13:34:52 -05001054certain parts of your queries for reuse at a later point in your
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001055script's execution. Normally, when an Query Builder call is completed,
Derek Jones8ede1a22011-10-05 13:34:52 -05001056all stored information is reset for the next call. With caching, you can
1057prevent this reset, and reuse information easily.
1058
1059Cached calls are cumulative. If you make 2 cached select() calls, and
1060then 2 uncached select() calls, this will result in 4 select() calls.
1061There are three Caching functions available:
1062
James L Parry42a7df62014-11-25 12:06:49 -08001063**$this->db->start_cache()**
Derek Jones8ede1a22011-10-05 13:34:52 -05001064
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001065This function must be called to begin caching. All Query Builder queries
Derek Jones8ede1a22011-10-05 13:34:52 -05001066of the correct type (see below for supported queries) are stored for
1067later use.
1068
James L Parry42a7df62014-11-25 12:06:49 -08001069**$this->db->stop_cache()**
Derek Jones8ede1a22011-10-05 13:34:52 -05001070
1071This function can be called to stop caching.
1072
James L Parry42a7df62014-11-25 12:06:49 -08001073**$this->db->flush_cache()**
Derek Jones8ede1a22011-10-05 13:34:52 -05001074
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001075This function deletes all items from the Query Builder cache.
Derek Jones8ede1a22011-10-05 13:34:52 -05001076
James L Parry42a7df62014-11-25 12:06:49 -08001077:returns: void
1078
1079An example of caching
1080---------------------
1081
Derek Jones8ede1a22011-10-05 13:34:52 -05001082Here's a usage example::
1083
1084 $this->db->start_cache();
1085 $this->db->select('field1');
1086 $this->db->stop_cache();
1087 $this->db->get('tablename');
1088 //Generates: SELECT `field1` FROM (`tablename`)
WanWizard7219c072011-12-28 14:09:05 +01001089
Derek Jones8ede1a22011-10-05 13:34:52 -05001090 $this->db->select('field2');
1091 $this->db->get('tablename');
1092 //Generates: SELECT `field1`, `field2` FROM (`tablename`)
WanWizard7219c072011-12-28 14:09:05 +01001093
Derek Jones8ede1a22011-10-05 13:34:52 -05001094 $this->db->flush_cache();
1095 $this->db->select('field2');
1096 $this->db->get('tablename');
1097 //Generates: SELECT `field2` FROM (`tablename`)
1098
1099
1100.. note:: The following statements can be cached: select, from, join,
1101 where, like, group_by, having, order_by, set
1102
1103
James L Parry42a7df62014-11-25 12:06:49 -08001104***********************
1105Resetting Query Builder
1106***********************
1107
Greg Akerffd24a42011-12-25 22:27:59 -06001108$this->db->reset_query()
James L Parry42a7df62014-11-25 12:06:49 -08001109------------------------
Kyle Farris48d8fb62011-10-14 17:59:49 -03001110
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001111Resetting Query Builder allows you to start fresh with your query without
WanWizard7219c072011-12-28 14:09:05 +01001112executing it first using a method like $this->db->get() or $this->db->insert().
1113Just like the methods that execute a query, this will *not* reset items you've
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001114cached using `Query Builder Caching`_.
Kyle Farris48d8fb62011-10-14 17:59:49 -03001115
Jamie Rumbelow7efad202012-02-19 12:37:00 +00001116This is useful in situations where you are using Query Builder to generate SQL
WanWizard7219c072011-12-28 14:09:05 +01001117(ex. ``$this->db->get_compiled_select()``) but then choose to, for instance,
Kyle Farris48d8fb62011-10-14 17:59:49 -03001118run the query::
1119
1120 // Note that the second parameter of the get_compiled_select method is FALSE
1121 $sql = $this->db->select(array('field1','field2'))
1122 ->where('field3',5)
1123 ->get_compiled_select('mytable', FALSE);
1124
1125 // ...
1126 // Do something crazy with the SQL code... like add it to a cron script for
1127 // later execution or something...
1128 // ...
1129
1130 $data = $this->db->get()->result_array();
1131
1132 // Would execute and return an array of results of the following query:
Andrey Andreev896d3e32014-01-07 17:13:25 +02001133 // SELECT field1, field1 from mytable where field3 = 5;
1134
1135.. note:: Double calls to ``get_compiled_select()`` while you're using the
1136 Query Builder Caching functionality and NOT resetting your queries
1137 will results in the cache being merged twice. That in turn will
James L Parry42a7df62014-11-25 12:06:49 -08001138 i.e. if you're caching a ``select()`` - select the same field twice.
1139
1140:returns: void