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 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [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 |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 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 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 22 | .. contents:: |
| 23 | :local: |
| 24 | :depth: 1 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 25 | |
| 26 | ************** |
| 27 | Selecting Data |
| 28 | ************** |
| 29 | |
| 30 | The following functions allow you to build SQL **SELECT** statements. |
| 31 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 32 | **$this->db->get()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 33 | |
| 34 | Runs the selection query and returns the result. Can be used by itself |
| 35 | to retrieve all records from a table:: |
| 36 | |
| 37 | $query = $this->db->get('mytable'); // Produces: SELECT * FROM mytable |
| 38 | |
| 39 | The second and third parameters enable you to set a limit and offset |
| 40 | clause:: |
| 41 | |
| 42 | $query = $this->db->get('mytable', 10, 20); |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 43 | |
| 44 | // Executes: SELECT * FROM mytable LIMIT 20, 10 |
| 45 | // (in MySQL. Other databases have slightly different syntax) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
| 47 | You'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'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 51 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 52 | foreach ($query->result() as $row) |
| 53 | { |
| 54 | echo $row->title; |
| 55 | } |
| 56 | |
| 57 | Please visit the :doc:`result functions <results>` page for a full |
| 58 | discussion regarding result generation. |
| 59 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 60 | **$this->db->get_compiled_select()** |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 61 | |
| 62 | 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] | 63 | the query. This method simply returns the SQL query as a string. |
| 64 | |
| 65 | Example:: |
| 66 | |
| 67 | $sql = $this->db->get_compiled_select('mytable'); |
| 68 | echo $sql; |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 69 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 70 | // Prints string: SELECT * FROM mytable |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 71 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 72 | The second parameter enables you to set whether or not the query builder query |
GDmac | 01e9fb1 | 2013-11-09 08:01:52 +0100 | [diff] [blame] | 73 | will be reset (by default it will be reset, just like when using `$this->db->get()`):: |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 74 | |
| 75 | echo $this->db->limit(10,20)->get_compiled_select('mytable', FALSE); |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 76 | |
| 77 | // Prints string: SELECT * FROM mytable LIMIT 20, 10 |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 78 | // (in MySQL. Other databases have slightly different syntax) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 79 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 80 | echo $this->db->select('title, content, date')->get_compiled_select(); |
| 81 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 82 | // Prints string: SELECT title, content, date FROM mytable LIMIT 20, 10 |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 83 | |
| 84 | The key thing to notice in the above example is that the second query did not |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 85 | utilize **$this->db->from()** and did not pass a table name into the first |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 86 | parameter. The reason for this outcome is because the query has not been |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 87 | executed using **$this->db->get()** which resets values or reset directly |
| 88 | using **$this->db->reset_query()**. |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 89 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 90 | **$this->db->get_where()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
| 92 | Identical to the above function except that it permits you to add a |
| 93 | "where" clause in the second parameter, instead of using the db->where() |
| 94 | function:: |
| 95 | |
| 96 | $query = $this->db->get_where('mytable', array('id' => $id), $limit, $offset); |
| 97 | |
| 98 | Please read the about the where function below for more information. |
| 99 | |
Greg Aker | ffd24a4 | 2011-12-25 22:27:59 -0600 | [diff] [blame] | 100 | .. note:: get_where() was formerly known as getwhere(), which has been removed |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 101 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 102 | **$this->db->select()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
| 104 | Permits you to write the SELECT portion of your query:: |
| 105 | |
| 106 | $this->db->select('title, content, date'); |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 107 | $query = $this->db->get('mytable'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 109 | // Executes: SELECT title, content, date FROM mytable |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
| 111 | .. note:: If you are selecting all (\*) from a table you do not need to |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 112 | use this function. When omitted, CodeIgniter assumes that you wish |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 113 | to select all fields and automatically adds 'SELECT \*'. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 115 | ``$this->db->select()`` accepts an optional second parameter. If you set it |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 116 | to FALSE, CodeIgniter will not try to protect your field or table names. |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 117 | This is useful if you need a compound select statement where automatic |
| 118 | escaping of fields may break them. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
| 120 | :: |
| 121 | |
Andrey Andreev | 954c4aa | 2017-07-21 11:51:37 +0300 | [diff] [blame] | 122 | $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] | 123 | $query = $this->db->get('mytable'); |
| 124 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 125 | **$this->db->select_max()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 127 | Writes a ``SELECT MAX(field)`` portion for your query. You can optionally |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | include a second parameter to rename the resulting field. |
| 129 | |
| 130 | :: |
| 131 | |
| 132 | $this->db->select_max('age'); |
| 133 | $query = $this->db->get('members'); // Produces: SELECT MAX(age) as age FROM members |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 134 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 135 | $this->db->select_max('age', 'member_age'); |
| 136 | $query = $this->db->get('members'); // Produces: SELECT MAX(age) as member_age FROM members |
| 137 | |
| 138 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 139 | **$this->db->select_min()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 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 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 151 | **$this->db->select_avg()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
| 153 | Writes a "SELECT AVG(field)" portion for your query. As with |
| 154 | select_max(), You can optionally include a second parameter to rename |
| 155 | the resulting field. |
| 156 | |
| 157 | :: |
| 158 | |
| 159 | $this->db->select_avg('age'); |
| 160 | $query = $this->db->get('members'); // Produces: SELECT AVG(age) as age FROM members |
| 161 | |
| 162 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 163 | **$this->db->select_sum()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 164 | |
| 165 | Writes a "SELECT SUM(field)" portion for your query. As with |
| 166 | select_max(), You can optionally include a second parameter to rename |
| 167 | the resulting field. |
| 168 | |
| 169 | :: |
| 170 | |
| 171 | $this->db->select_sum('age'); |
| 172 | $query = $this->db->get('members'); // Produces: SELECT SUM(age) as age FROM members |
| 173 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 174 | **$this->db->from()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
| 176 | Permits you to write the FROM portion of your query:: |
| 177 | |
| 178 | $this->db->select('title, content, date'); |
| 179 | $this->db->from('mytable'); |
| 180 | $query = $this->db->get(); // Produces: SELECT title, content, date FROM mytable |
| 181 | |
| 182 | .. note:: As shown earlier, the FROM portion of your query can be specified |
| 183 | in the $this->db->get() function, so use whichever method you prefer. |
| 184 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 185 | **$this->db->join()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 186 | |
| 187 | Permits you to write the JOIN portion of your query:: |
| 188 | |
| 189 | $this->db->select('*'); |
| 190 | $this->db->from('blogs'); |
| 191 | $this->db->join('comments', 'comments.id = blogs.id'); |
| 192 | $query = $this->db->get(); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 193 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 194 | // Produces: |
kenjis | c35d2c9 | 2011-10-26 17:09:17 +0900 | [diff] [blame] | 195 | // SELECT * FROM blogs JOIN comments ON comments.id = blogs.id |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 196 | |
| 197 | Multiple function calls can be made if you need several joins in one |
| 198 | query. |
| 199 | |
| 200 | If you need a specific type of JOIN you can specify it via the third |
| 201 | parameter of the function. Options are: left, right, outer, inner, left |
| 202 | outer, and right outer. |
| 203 | |
| 204 | :: |
| 205 | |
| 206 | $this->db->join('comments', 'comments.id = blogs.id', 'left'); |
| 207 | // Produces: LEFT JOIN comments ON comments.id = blogs.id |
| 208 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 209 | ************************* |
| 210 | Looking for Specific Data |
| 211 | ************************* |
| 212 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 213 | **$this->db->where()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 214 | |
| 215 | This function enables you to set **WHERE** clauses using one of four |
| 216 | methods: |
| 217 | |
| 218 | .. note:: All values passed to this function are escaped automatically, |
| 219 | producing safer queries. |
| 220 | |
| 221 | #. **Simple key/value method:** |
| 222 | |
| 223 | :: |
| 224 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 225 | $this->db->where('name', $name); // Produces: WHERE name = 'Joe' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 226 | |
| 227 | Notice that the equal sign is added for you. |
| 228 | |
| 229 | If you use multiple function calls they will be chained together with |
| 230 | AND between them: |
| 231 | |
| 232 | :: |
| 233 | |
| 234 | $this->db->where('name', $name); |
| 235 | $this->db->where('title', $title); |
| 236 | $this->db->where('status', $status); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 237 | // WHERE name = 'Joe' AND title = 'boss' AND status = 'active' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 238 | |
| 239 | #. **Custom key/value method:** |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 240 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 241 | You can include an operator in the first parameter in order to |
| 242 | control the comparison: |
| 243 | |
| 244 | :: |
| 245 | |
| 246 | $this->db->where('name !=', $name); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 247 | $this->db->where('id <', $id); // Produces: WHERE name != 'Joe' AND id < 45 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 248 | |
| 249 | #. **Associative array method:** |
| 250 | |
| 251 | :: |
| 252 | |
| 253 | $array = array('name' => $name, 'title' => $title, 'status' => $status); |
| 254 | $this->db->where($array); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 255 | // Produces: WHERE name = 'Joe' AND title = 'boss' AND status = 'active' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 256 | |
| 257 | You can include your own operators using this method as well: |
| 258 | |
| 259 | :: |
| 260 | |
| 261 | $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date); |
| 262 | $this->db->where($array); |
| 263 | |
| 264 | #. **Custom string:** |
| 265 | You can write your own clauses manually:: |
| 266 | |
| 267 | $where = "name='Joe' AND status='boss' OR status='active'"; |
| 268 | $this->db->where($where); |
| 269 | |
| 270 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 271 | ``$this->db->where()`` accepts an optional third parameter. If you set it to |
| 272 | FALSE, CodeIgniter will not try to protect your field or table names. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 273 | |
| 274 | :: |
| 275 | |
| 276 | $this->db->where('MATCH (field) AGAINST ("value")', NULL, FALSE); |
| 277 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 278 | **$this->db->or_where()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 279 | |
| 280 | This function is identical to the one above, except that multiple |
| 281 | instances are joined by OR:: |
| 282 | |
| 283 | $this->db->where('name !=', $name); |
| 284 | $this->db->or_where('id >', $id); // Produces: WHERE name != 'Joe' OR id > 50 |
| 285 | |
| 286 | .. note:: or_where() was formerly known as orwhere(), which has been |
| 287 | removed. |
| 288 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 289 | **$this->db->where_in()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 290 | |
| 291 | Generates a WHERE field IN ('item', 'item') SQL query joined with AND if |
| 292 | appropriate |
| 293 | |
| 294 | :: |
| 295 | |
| 296 | $names = array('Frank', 'Todd', 'James'); |
| 297 | $this->db->where_in('username', $names); |
| 298 | // Produces: WHERE username IN ('Frank', 'Todd', 'James') |
| 299 | |
| 300 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 301 | **$this->db->or_where_in()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 302 | |
| 303 | Generates a WHERE field IN ('item', 'item') SQL query joined with OR if |
| 304 | appropriate |
| 305 | |
| 306 | :: |
| 307 | |
| 308 | $names = array('Frank', 'Todd', 'James'); |
| 309 | $this->db->or_where_in('username', $names); |
| 310 | // Produces: OR username IN ('Frank', 'Todd', 'James') |
| 311 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 312 | **$this->db->where_not_in()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 313 | |
| 314 | Generates a WHERE field NOT IN ('item', 'item') SQL query joined with |
| 315 | AND if appropriate |
| 316 | |
| 317 | :: |
| 318 | |
| 319 | $names = array('Frank', 'Todd', 'James'); |
| 320 | $this->db->where_not_in('username', $names); |
| 321 | // Produces: WHERE username NOT IN ('Frank', 'Todd', 'James') |
| 322 | |
| 323 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 324 | **$this->db->or_where_not_in()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 325 | |
| 326 | Generates a WHERE field NOT IN ('item', 'item') SQL query joined with OR |
| 327 | if appropriate |
| 328 | |
| 329 | :: |
| 330 | |
| 331 | $names = array('Frank', 'Todd', 'James'); |
| 332 | $this->db->or_where_not_in('username', $names); |
| 333 | // Produces: OR username NOT IN ('Frank', 'Todd', 'James') |
| 334 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 335 | ************************ |
| 336 | Looking for Similar Data |
| 337 | ************************ |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 338 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 339 | **$this->db->like()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 340 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 341 | This method enables you to generate **LIKE** clauses, useful for doing |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 342 | searches. |
| 343 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 344 | .. note:: All values passed to this method are escaped automatically. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 345 | |
| 346 | #. **Simple key/value method:** |
| 347 | |
| 348 | :: |
| 349 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 350 | $this->db->like('title', 'match'); |
| 351 | // Produces: WHERE `title` LIKE '%match%' ESCAPE '!' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 352 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 353 | If you use multiple method calls they will be chained together with |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 354 | AND between them:: |
| 355 | |
| 356 | $this->db->like('title', 'match'); |
| 357 | $this->db->like('body', 'match'); |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 358 | // WHERE `title` LIKE '%match%' ESCAPE '!' AND `body` LIKE '%match% ESCAPE '!' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 359 | |
| 360 | If you want to control where the wildcard (%) is placed, you can use |
Andrey Andreev | c450183 | 2018-09-05 16:54:42 +0300 | [diff] [blame] | 361 | an optional third argument. Your options are 'before', 'after', 'none' and |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 362 | 'both' (which is the default). |
| 363 | |
| 364 | :: |
| 365 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 366 | $this->db->like('title', 'match', 'before'); // Produces: WHERE `title` LIKE '%match' ESCAPE '!' |
| 367 | $this->db->like('title', 'match', 'after'); // Produces: WHERE `title` LIKE 'match%' ESCAPE '!' |
Andrey Andreev | c450183 | 2018-09-05 16:54:42 +0300 | [diff] [blame] | 368 | $this->db->like('title', 'match', 'none'); // Produces: WHERE `title` LIKE 'match' ESCAPE '!' |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 369 | $this->db->like('title', 'match', 'both'); // Produces: WHERE `title` LIKE '%match%' ESCAPE '!' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 370 | |
| 371 | #. **Associative array method:** |
| 372 | |
| 373 | :: |
| 374 | |
| 375 | $array = array('title' => $match, 'page1' => $match, 'page2' => $match); |
| 376 | $this->db->like($array); |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 377 | // WHERE `title` LIKE '%match%' ESCAPE '!' AND `page1` LIKE '%match%' ESCAPE '!' AND `page2` LIKE '%match%' ESCAPE '!' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 378 | |
| 379 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 380 | **$this->db->or_like()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 381 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 382 | This method is identical to the one above, except that multiple |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 383 | instances are joined by OR:: |
| 384 | |
| 385 | $this->db->like('title', 'match'); $this->db->or_like('body', $match); |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 386 | // WHERE `title` LIKE '%match%' ESCAPE '!' OR `body` LIKE '%match%' ESCAPE '!' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 387 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 388 | .. note:: ``or_like()`` was formerly known as ``orlike()``, which has been removed. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 389 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 390 | **$this->db->not_like()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 391 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 392 | This method is identical to ``like()``, except that it generates |
| 393 | NOT LIKE statements:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 394 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 395 | $this->db->not_like('title', 'match'); // WHERE `title` NOT LIKE '%match% ESCAPE '!' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 396 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 397 | **$this->db->or_not_like()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 398 | |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 399 | This method is identical to ``not_like()``, except that multiple |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 400 | instances are joined by OR:: |
| 401 | |
| 402 | $this->db->like('title', 'match'); |
| 403 | $this->db->or_not_like('body', 'match'); |
Andrey Andreev | 4173823 | 2012-11-30 00:13:17 +0200 | [diff] [blame] | 404 | // WHERE `title` LIKE '%match% OR `body` NOT LIKE '%match%' ESCAPE '!' |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 405 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 406 | **$this->db->group_by()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 407 | |
| 408 | Permits you to write the GROUP BY portion of your query:: |
| 409 | |
| 410 | $this->db->group_by("title"); // Produces: GROUP BY title |
| 411 | |
| 412 | You can also pass an array of multiple values as well:: |
| 413 | |
| 414 | $this->db->group_by(array("title", "date")); // Produces: GROUP BY title, date |
| 415 | |
| 416 | .. note:: group_by() was formerly known as groupby(), which has been |
| 417 | removed. |
| 418 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 419 | **$this->db->distinct()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 420 | |
| 421 | Adds the "DISTINCT" keyword to a query |
| 422 | |
| 423 | :: |
| 424 | |
| 425 | $this->db->distinct(); |
| 426 | $this->db->get('table'); // Produces: SELECT DISTINCT * FROM table |
| 427 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 428 | **$this->db->having()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 429 | |
| 430 | Permits you to write the HAVING portion of your query. There are 2 |
| 431 | possible syntaxes, 1 argument or 2:: |
| 432 | |
| 433 | $this->db->having('user_id = 45'); // Produces: HAVING user_id = 45 |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 434 | $this->db->having('user_id', 45); // Produces: HAVING user_id = 45 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 435 | |
| 436 | You can also pass an array of multiple values as well:: |
| 437 | |
| 438 | $this->db->having(array('title =' => 'My Title', 'id <' => $id)); |
| 439 | // Produces: HAVING title = 'My Title', id < 45 |
| 440 | |
| 441 | |
| 442 | If you are using a database that CodeIgniter escapes queries for, you |
| 443 | can prevent escaping content by passing an optional third argument, and |
| 444 | setting it to FALSE. |
| 445 | |
| 446 | :: |
| 447 | |
| 448 | $this->db->having('user_id', 45); // Produces: HAVING `user_id` = 45 in some databases such as MySQL |
| 449 | $this->db->having('user_id', 45, FALSE); // Produces: HAVING user_id = 45 |
| 450 | |
| 451 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 452 | **$this->db->or_having()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 453 | |
| 454 | Identical to having(), only separates multiple clauses with "OR". |
| 455 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 456 | **************** |
| 457 | Ordering results |
| 458 | **************** |
| 459 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 460 | **$this->db->order_by()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 461 | |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 462 | Lets you set an ORDER BY clause. |
| 463 | |
| 464 | The first parameter contains the name of the column you would like to order by. |
| 465 | |
| 466 | The second parameter lets you set the direction of the result. |
| 467 | Options are **ASC**, **DESC** AND **RANDOM**. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 468 | |
| 469 | :: |
| 470 | |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 471 | $this->db->order_by('title', 'DESC'); |
| 472 | // Produces: ORDER BY `title` DESC |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 473 | |
| 474 | You can also pass your own string in the first parameter:: |
| 475 | |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 476 | $this->db->order_by('title DESC, name ASC'); |
| 477 | // Produces: ORDER BY `title` DESC, `name` ASC |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 478 | |
| 479 | Or multiple function calls can be made if you need multiple fields. |
| 480 | |
| 481 | :: |
| 482 | |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 483 | $this->db->order_by('title', 'DESC'); |
| 484 | $this->db->order_by('name', 'ASC'); |
| 485 | // Produces: ORDER BY `title` DESC, `name` ASC |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 486 | |
Andrey Andreev | 98e46cf | 2012-11-13 03:01:42 +0200 | [diff] [blame] | 487 | If you choose the **RANDOM** direction option, then the first parameters will |
| 488 | be ignored, unless you specify a numeric seed value. |
| 489 | |
| 490 | :: |
| 491 | |
| 492 | $this->db->order_by('title', 'RANDOM'); |
| 493 | // Produces: ORDER BY RAND() |
| 494 | |
| 495 | $this->db->order_by(42, 'RANDOM'); |
| 496 | // Produces: ORDER BY RAND(42) |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 497 | |
| 498 | .. note:: order_by() was formerly known as orderby(), which has been |
| 499 | removed. |
| 500 | |
Andrey Andreev | 0dfb62f | 2012-10-30 11:37:15 +0200 | [diff] [blame] | 501 | .. note:: Random ordering is not currently supported in Oracle and |
| 502 | will default to ASC instead. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 503 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 504 | **************************** |
| 505 | Limiting or Counting Results |
| 506 | **************************** |
| 507 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 508 | **$this->db->limit()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 509 | |
| 510 | Lets you limit the number of rows you would like returned by the query:: |
| 511 | |
| 512 | $this->db->limit(10); // Produces: LIMIT 10 |
| 513 | |
| 514 | The second parameter lets you set a result offset. |
| 515 | |
| 516 | :: |
| 517 | |
| 518 | $this->db->limit(10, 20); // Produces: LIMIT 20, 10 (in MySQL. Other databases have slightly different syntax) |
| 519 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 520 | **$this->db->count_all_results()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 521 | |
| 522 | Permits you to determine the number of rows in a particular Active |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 523 | Record query. Queries will accept Query Builder restrictors such as |
Andrey Andreev | ff806f9 | 2015-03-16 17:05:25 +0200 | [diff] [blame] | 524 | ``where()``, ``or_where()``, ``like()``, ``or_like()``, etc. Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 525 | |
| 526 | echo $this->db->count_all_results('my_table'); // Produces an integer, like 25 |
| 527 | $this->db->like('title', 'match'); |
| 528 | $this->db->from('my_table'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 529 | echo $this->db->count_all_results(); // Produces an integer, like 17 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 530 | |
Andrey Andreev | ff806f9 | 2015-03-16 17:05:25 +0200 | [diff] [blame] | 531 | However, this method also resets any field values that you may have passed |
| 532 | to ``select()``. If you need to keep them, you can pass ``FALSE`` as the |
| 533 | second parameter:: |
yaoshanliang | 2f16405 | 2015-03-16 16:48:15 +0800 | [diff] [blame] | 534 | |
Andrey Andreev | ff806f9 | 2015-03-16 17:05:25 +0200 | [diff] [blame] | 535 | echo $this->db->count_all_results('my_table', FALSE); |
yaoshanliang | 2f16405 | 2015-03-16 16:48:15 +0800 | [diff] [blame] | 536 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 537 | **$this->db->count_all()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 538 | |
| 539 | Permits you to determine the number of rows in a particular table. |
| 540 | Submit the table name in the first parameter. Example:: |
| 541 | |
| 542 | echo $this->db->count_all('my_table'); // Produces an integer, like 25 |
| 543 | |
| 544 | ************** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 545 | Query grouping |
| 546 | ************** |
| 547 | |
| 548 | 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] | 549 | you to create queries with complex WHERE clauses. Nested groups are supported. Example:: |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 550 | |
| 551 | $this->db->select('*')->from('my_table') |
| 552 | ->group_start() |
| 553 | ->where('a', 'a') |
| 554 | ->or_group_start() |
| 555 | ->where('b', 'b') |
| 556 | ->where('c', 'c') |
| 557 | ->group_end() |
| 558 | ->group_end() |
| 559 | ->where('d', 'd') |
| 560 | ->get(); |
| 561 | |
| 562 | // Generates: |
| 563 | // SELECT * FROM (`my_table`) WHERE ( `a` = 'a' OR ( `b` = 'b' AND `c` = 'c' ) ) AND `d` = 'd' |
| 564 | |
| 565 | .. note:: groups need to be balanced, make sure every group_start() is matched by a group_end(). |
| 566 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 567 | **$this->db->group_start()** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 568 | |
| 569 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query. |
| 570 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 571 | **$this->db->or_group_start()** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 572 | |
| 573 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR'. |
| 574 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 575 | **$this->db->not_group_start()** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 576 | |
| 577 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'NOT'. |
| 578 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 579 | **$this->db->or_not_group_start()** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 580 | |
| 581 | Starts a new group by adding an opening parenthesis to the WHERE clause of the query, prefixing it with 'OR NOT'. |
| 582 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 583 | **$this->db->group_end()** |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 584 | |
| 585 | Ends the current group by adding an closing parenthesis to the WHERE clause of the query. |
| 586 | |
| 587 | ************** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 588 | Inserting Data |
| 589 | ************** |
| 590 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 591 | **$this->db->insert()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 592 | |
| 593 | Generates an insert string based on the data you supply, and runs the |
| 594 | query. You can either pass an **array** or an **object** to the |
| 595 | function. Here is an example using an array:: |
| 596 | |
| 597 | $data = array( |
| 598 | 'title' => 'My title', |
| 599 | 'name' => 'My Name', |
| 600 | 'date' => 'My date' |
| 601 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 602 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 603 | $this->db->insert('mytable', $data); |
| 604 | // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') |
| 605 | |
| 606 | The first parameter will contain the table name, the second is an |
| 607 | associative array of values. |
| 608 | |
| 609 | Here is an example using an object:: |
| 610 | |
| 611 | /* |
| 612 | class Myclass { |
vlakoff | ff3f7de | 2012-06-16 14:21:32 +0200 | [diff] [blame] | 613 | public $title = 'My Title'; |
| 614 | public $content = 'My Content'; |
| 615 | public $date = 'My Date'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 616 | } |
| 617 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 618 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 619 | $object = new Myclass; |
| 620 | $this->db->insert('mytable', $object); |
| 621 | // Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date') |
| 622 | |
| 623 | The first parameter will contain the table name, the second is an |
| 624 | object. |
| 625 | |
| 626 | .. note:: All values are escaped automatically producing safer queries. |
| 627 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 628 | **$this->db->get_compiled_insert()** |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 629 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 630 | Compiles the insertion query just like $this->db->insert() but does not |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 631 | *run* the query. This method simply returns the SQL query as a string. |
| 632 | |
| 633 | Example:: |
| 634 | |
| 635 | $data = array( |
| 636 | 'title' => 'My title', |
| 637 | 'name' => 'My Name', |
| 638 | 'date' => 'My date' |
| 639 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 640 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 641 | $sql = $this->db->set($data)->get_compiled_insert('mytable'); |
| 642 | echo $sql; |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 643 | |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 644 | // Produces string: INSERT INTO mytable (`title`, `name`, `date`) VALUES ('My title', 'My name', 'My date') |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 645 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 646 | The second parameter enables you to set whether or not the query builder query |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 647 | will be reset (by default it will be--just like $this->db->insert()):: |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 648 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 649 | echo $this->db->set('title', 'My Title')->get_compiled_insert('mytable', FALSE); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 650 | |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 651 | // Produces string: INSERT INTO mytable (`title`) VALUES ('My Title') |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 652 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 653 | echo $this->db->set('content', 'My Content')->get_compiled_insert(); |
| 654 | |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 655 | // Produces string: INSERT INTO mytable (`title`, `content`) VALUES ('My Title', 'My Content') |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 656 | |
| 657 | The key thing to notice in the above example is that the second query did not |
Andrey Andreev | cdf3a9a | 2018-03-15 16:58:31 +0200 | [diff] [blame] | 658 | utilize `$this->db->from()` nor did it pass a table name into the first |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 659 | parameter. The reason this worked is because the query has not been executed |
Andrey Andreev | 6bdfa42 | 2013-12-19 15:36:01 +0200 | [diff] [blame] | 660 | using `$this->db->insert()` which resets values or reset directly using |
| 661 | `$this->db->reset_query()`. |
| 662 | |
| 663 | .. note:: This method doesn't work for batched inserts. |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 664 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 665 | **$this->db->insert_batch()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 666 | |
| 667 | Generates an insert string based on the data you supply, and runs the |
| 668 | query. You can either pass an **array** or an **object** to the |
| 669 | function. Here is an example using an array:: |
| 670 | |
| 671 | $data = array( |
| 672 | array( |
| 673 | 'title' => 'My title', |
| 674 | 'name' => 'My Name', |
| 675 | 'date' => 'My date' |
| 676 | ), |
| 677 | array( |
| 678 | 'title' => 'Another title', |
| 679 | 'name' => 'Another Name', |
| 680 | 'date' => 'Another date' |
| 681 | ) |
| 682 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 683 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 684 | $this->db->insert_batch('mytable', $data); |
| 685 | // Produces: INSERT INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date'), ('Another title', 'Another name', 'Another date') |
| 686 | |
| 687 | The first parameter will contain the table name, the second is an |
| 688 | associative array of values. |
| 689 | |
| 690 | .. note:: All values are escaped automatically producing safer queries. |
| 691 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 692 | ************* |
| 693 | Updating Data |
| 694 | ************* |
| 695 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 696 | **$this->db->replace()** |
Andrey Andreev | 04c50f5 | 2012-10-24 23:05:25 +0300 | [diff] [blame] | 697 | |
| 698 | This method executes a REPLACE statement, which is basically the SQL |
| 699 | standard for (optional) DELETE + INSERT, using *PRIMARY* and *UNIQUE* |
| 700 | keys as the determining factor. |
| 701 | In our case, it will save you from the need to implement complex |
| 702 | logics with different combinations of ``select()``, ``update()``, |
| 703 | ``delete()`` and ``insert()`` calls. |
| 704 | |
| 705 | Example:: |
| 706 | |
| 707 | $data = array( |
| 708 | 'title' => 'My title', |
| 709 | 'name' => 'My Name', |
| 710 | 'date' => 'My date' |
| 711 | ); |
| 712 | |
| 713 | $this->db->replace('table', $data); |
| 714 | |
| 715 | // Executes: REPLACE INTO mytable (title, name, date) VALUES ('My title', 'My name', 'My date') |
| 716 | |
| 717 | In the above example, if we assume that the *title* field is our primary |
| 718 | key, then if a row containing 'My title' as the *title* value, that row |
| 719 | will be deleted with our new row data replacing it. |
| 720 | |
| 721 | Usage of the ``set()`` method is also allowed and all fields are |
| 722 | automatically escaped, just like with ``insert()``. |
| 723 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 724 | **$this->db->set()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 725 | |
| 726 | This function enables you to set values for inserts or updates. |
| 727 | |
| 728 | **It can be used instead of passing a data array directly to the insert |
| 729 | or update functions:** |
| 730 | |
| 731 | :: |
| 732 | |
| 733 | $this->db->set('name', $name); |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 734 | $this->db->insert('mytable'); // Produces: INSERT INTO mytable (`name`) VALUES ('{$name}') |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 735 | |
| 736 | If you use multiple function called they will be assembled properly |
| 737 | based on whether you are doing an insert or an update:: |
| 738 | |
| 739 | $this->db->set('name', $name); |
| 740 | $this->db->set('title', $title); |
| 741 | $this->db->set('status', $status); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 742 | $this->db->insert('mytable'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 743 | |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 744 | **set()** will also accept an optional third parameter (``$escape``), that |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 745 | will prevent data from being escaped if set to FALSE. To illustrate the |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 746 | difference, here is ``set()`` used both with and without the escape |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 747 | parameter. |
| 748 | |
| 749 | :: |
| 750 | |
| 751 | $this->db->set('field', 'field+1', FALSE); |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 752 | $this->db->where('id', 2); |
| 753 | $this->db->update('mytable'); // gives UPDATE mytable SET field = field+1 WHERE id = 2 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 754 | |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 755 | $this->db->set('field', 'field+1'); |
| 756 | $this->db->where('id', 2); |
| 757 | $this->db->update('mytable'); // gives UPDATE `mytable` SET `field` = 'field+1' WHERE `id` = 2 |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 758 | |
| 759 | You can also pass an associative array to this function:: |
| 760 | |
| 761 | $array = array( |
| 762 | 'name' => $name, |
| 763 | 'title' => $title, |
| 764 | 'status' => $status |
| 765 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 766 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 767 | $this->db->set($array); |
| 768 | $this->db->insert('mytable'); |
| 769 | |
| 770 | Or an object:: |
| 771 | |
| 772 | /* |
| 773 | class Myclass { |
vlakoff | ff3f7de | 2012-06-16 14:21:32 +0200 | [diff] [blame] | 774 | public $title = 'My Title'; |
| 775 | public $content = 'My Content'; |
| 776 | public $date = 'My Date'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 777 | } |
| 778 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 779 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 780 | $object = new Myclass; |
| 781 | $this->db->set($object); |
| 782 | $this->db->insert('mytable'); |
| 783 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 784 | **$this->db->update()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 785 | |
| 786 | Generates an update string and runs the query based on the data you |
| 787 | supply. You can pass an **array** or an **object** to the function. Here |
| 788 | is an example using an array:: |
| 789 | |
| 790 | $data = array( |
| 791 | 'title' => $title, |
| 792 | 'name' => $name, |
| 793 | 'date' => $date |
| 794 | ); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 795 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 796 | $this->db->where('id', $id); |
| 797 | $this->db->update('mytable', $data); |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 798 | // Produces: |
| 799 | // |
| 800 | // UPDATE mytable |
| 801 | // SET title = '{$title}', name = '{$name}', date = '{$date}' |
| 802 | // WHERE id = $id |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 803 | |
| 804 | Or you can supply an object:: |
| 805 | |
| 806 | /* |
| 807 | class Myclass { |
vlakoff | ff3f7de | 2012-06-16 14:21:32 +0200 | [diff] [blame] | 808 | public $title = 'My Title'; |
| 809 | public $content = 'My Content'; |
| 810 | public $date = 'My Date'; |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 811 | } |
| 812 | */ |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 813 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 814 | $object = new Myclass; |
| 815 | $this->db->where('id', $id); |
| 816 | $this->db->update('mytable', $object); |
Andrey Andreev | d80ecd6 | 2015-07-27 21:53:22 +0300 | [diff] [blame] | 817 | // Produces: |
| 818 | // |
| 819 | // UPDATE `mytable` |
| 820 | // SET `title` = '{$title}', `name` = '{$name}', `date` = '{$date}' |
| 821 | // WHERE id = `$id` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 822 | |
| 823 | .. note:: All values are escaped automatically producing safer queries. |
| 824 | |
| 825 | You'll notice the use of the $this->db->where() function, enabling you |
| 826 | to set the WHERE clause. You can optionally pass this information |
| 827 | directly into the update function as a string:: |
| 828 | |
| 829 | $this->db->update('mytable', $data, "id = 4"); |
| 830 | |
| 831 | Or as an array:: |
| 832 | |
| 833 | $this->db->update('mytable', $data, array('id' => $id)); |
| 834 | |
| 835 | You may also use the $this->db->set() function described above when |
| 836 | performing updates. |
| 837 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 838 | **$this->db->update_batch()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 839 | |
| 840 | Generates an update string based on the data you supply, and runs the query. |
| 841 | You can either pass an **array** or an **object** to the function. |
| 842 | Here is an example using an array:: |
| 843 | |
| 844 | $data = array( |
| 845 | array( |
| 846 | 'title' => 'My title' , |
| 847 | 'name' => 'My Name 2' , |
| 848 | 'date' => 'My date 2' |
| 849 | ), |
| 850 | array( |
| 851 | 'title' => 'Another title' , |
| 852 | 'name' => 'Another Name 2' , |
| 853 | 'date' => 'Another date 2' |
| 854 | ) |
| 855 | ); |
| 856 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 857 | $this->db->update_batch('mytable', $data, 'title'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 858 | |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 859 | // Produces: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 860 | // UPDATE `mytable` SET `name` = CASE |
| 861 | // WHEN `title` = 'My title' THEN 'My Name 2' |
| 862 | // WHEN `title` = 'Another title' THEN 'Another Name 2' |
| 863 | // ELSE `name` END, |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 864 | // `date` = CASE |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 865 | // WHEN `title` = 'My title' THEN 'My date 2' |
| 866 | // WHEN `title` = 'Another title' THEN 'Another date 2' |
| 867 | // ELSE `date` END |
| 868 | // WHERE `title` IN ('My title','Another title') |
| 869 | |
| 870 | The first parameter will contain the table name, the second is an associative |
| 871 | array of values, the third parameter is the where key. |
| 872 | |
| 873 | .. note:: All values are escaped automatically producing safer queries. |
| 874 | |
Andrey Andreev | 9f808b0 | 2012-10-24 17:38:48 +0300 | [diff] [blame] | 875 | .. note:: ``affected_rows()`` won't give you proper results with this method, |
| 876 | due to the very nature of how it works. Instead, ``update_batch()`` |
| 877 | returns the number of rows affected. |
| 878 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 879 | **$this->db->get_compiled_update()** |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 880 | |
| 881 | This works exactly the same way as ``$this->db->get_compiled_insert()`` except |
| 882 | that it produces an UPDATE SQL string instead of an INSERT SQL string. |
| 883 | |
Andrey Andreev | 6bdfa42 | 2013-12-19 15:36:01 +0200 | [diff] [blame] | 884 | For more information view documentation for `$this->db->get_compiled_insert()`. |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 885 | |
Andrey Andreev | 6bdfa42 | 2013-12-19 15:36:01 +0200 | [diff] [blame] | 886 | .. note:: This method doesn't work for batched updates. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 887 | |
| 888 | ************* |
| 889 | Deleting Data |
| 890 | ************* |
| 891 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 892 | **$this->db->delete()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 893 | |
| 894 | Generates a delete SQL string and runs the query. |
| 895 | |
| 896 | :: |
| 897 | |
| 898 | $this->db->delete('mytable', array('id' => $id)); // Produces: // DELETE FROM mytable // WHERE id = $id |
| 899 | |
| 900 | The first parameter is the table name, the second is the where clause. |
| 901 | You can also use the where() or or_where() functions instead of passing |
| 902 | the data to the second parameter of the function:: |
| 903 | |
| 904 | $this->db->where('id', $id); |
| 905 | $this->db->delete('mytable'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 906 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 907 | // Produces: |
| 908 | // DELETE FROM mytable |
| 909 | // WHERE id = $id |
| 910 | |
| 911 | |
| 912 | An array of table names can be passed into delete() if you would like to |
| 913 | delete data from more than 1 table. |
| 914 | |
| 915 | :: |
| 916 | |
| 917 | $tables = array('table1', 'table2', 'table3'); |
| 918 | $this->db->where('id', '5'); |
| 919 | $this->db->delete($tables); |
| 920 | |
| 921 | |
| 922 | If you want to delete all data from a table, you can use the truncate() |
| 923 | function, or empty_table(). |
| 924 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 925 | **$this->db->empty_table()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 926 | |
| 927 | Generates a delete SQL string and runs the |
| 928 | query.:: |
| 929 | |
kenjis | c35d2c9 | 2011-10-26 17:09:17 +0900 | [diff] [blame] | 930 | $this->db->empty_table('mytable'); // Produces: DELETE FROM mytable |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 931 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 932 | **$this->db->truncate()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 933 | |
| 934 | Generates a truncate SQL string and runs the query. |
| 935 | |
| 936 | :: |
| 937 | |
| 938 | $this->db->from('mytable'); |
kenjis | c35d2c9 | 2011-10-26 17:09:17 +0900 | [diff] [blame] | 939 | $this->db->truncate(); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 940 | |
| 941 | // or |
| 942 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 943 | $this->db->truncate('mytable'); |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 944 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 945 | // Produce: |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 946 | // TRUNCATE mytable |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 947 | |
| 948 | .. note:: If the TRUNCATE command isn't available, truncate() will |
| 949 | execute as "DELETE FROM table". |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 950 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 951 | **$this->db->get_compiled_delete()** |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 952 | |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 953 | This works exactly the same way as ``$this->db->get_compiled_insert()`` except |
| 954 | that it produces a DELETE SQL string instead of an INSERT SQL string. |
| 955 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 956 | For more information view documentation for $this->db->get_compiled_insert(). |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 957 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 958 | *************** |
| 959 | Method Chaining |
| 960 | *************** |
| 961 | |
| 962 | Method chaining allows you to simplify your syntax by connecting |
| 963 | multiple functions. Consider this example:: |
| 964 | |
| 965 | $query = $this->db->select('title') |
Timothy Warren | e464b39 | 2012-03-13 14:09:31 -0400 | [diff] [blame] | 966 | ->where('id', $id) |
| 967 | ->limit(10, 20) |
| 968 | ->get('mytable'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 969 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 970 | .. _ar-caching: |
| 971 | |
| 972 | ********************* |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 973 | Query Builder Caching |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 974 | ********************* |
| 975 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 976 | While not "true" caching, Query Builder enables you to save (or "cache") |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 977 | 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] | 978 | script's execution. Normally, when an Query Builder call is completed, |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 979 | all stored information is reset for the next call. With caching, you can |
| 980 | prevent this reset, and reuse information easily. |
| 981 | |
| 982 | Cached calls are cumulative. If you make 2 cached select() calls, and |
| 983 | then 2 uncached select() calls, this will result in 4 select() calls. |
| 984 | There are three Caching functions available: |
| 985 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 986 | **$this->db->start_cache()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 987 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 988 | This function must be called to begin caching. All Query Builder queries |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 989 | of the correct type (see below for supported queries) are stored for |
| 990 | later use. |
| 991 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 992 | **$this->db->stop_cache()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 993 | |
| 994 | This function can be called to stop caching. |
| 995 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 996 | **$this->db->flush_cache()** |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 997 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 998 | This function deletes all items from the Query Builder cache. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 999 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 1000 | An example of caching |
| 1001 | --------------------- |
| 1002 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1003 | Here's a usage example:: |
| 1004 | |
| 1005 | $this->db->start_cache(); |
| 1006 | $this->db->select('field1'); |
| 1007 | $this->db->stop_cache(); |
| 1008 | $this->db->get('tablename'); |
| 1009 | //Generates: SELECT `field1` FROM (`tablename`) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1010 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1011 | $this->db->select('field2'); |
| 1012 | $this->db->get('tablename'); |
| 1013 | //Generates: SELECT `field1`, `field2` FROM (`tablename`) |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1014 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1015 | $this->db->flush_cache(); |
| 1016 | $this->db->select('field2'); |
| 1017 | $this->db->get('tablename'); |
| 1018 | //Generates: SELECT `field2` FROM (`tablename`) |
| 1019 | |
| 1020 | |
| 1021 | .. note:: The following statements can be cached: select, from, join, |
Andrey Andreev | 97d5154 | 2015-10-19 11:28:11 +0300 | [diff] [blame] | 1022 | where, like, group_by, having, order_by |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1023 | |
| 1024 | |
James L Parry | 42a7df6 | 2014-11-25 12:06:49 -0800 | [diff] [blame] | 1025 | *********************** |
| 1026 | Resetting Query Builder |
| 1027 | *********************** |
| 1028 | |
James L Parry | 141288d | 2014-12-06 01:45:12 -0800 | [diff] [blame] | 1029 | **$this->db->reset_query()** |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 1030 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1031 | Resetting Query Builder allows you to start fresh with your query without |
WanWizard | 7219c07 | 2011-12-28 14:09:05 +0100 | [diff] [blame] | 1032 | executing it first using a method like $this->db->get() or $this->db->insert(). |
| 1033 | 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] | 1034 | cached using `Query Builder Caching`_. |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 1035 | |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 1036 | 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] | 1037 | (ex. ``$this->db->get_compiled_select()``) but then choose to, for instance, |
Kyle Farris | 48d8fb6 | 2011-10-14 17:59:49 -0300 | [diff] [blame] | 1038 | run the query:: |
| 1039 | |
| 1040 | // Note that the second parameter of the get_compiled_select method is FALSE |
| 1041 | $sql = $this->db->select(array('field1','field2')) |
| 1042 | ->where('field3',5) |
| 1043 | ->get_compiled_select('mytable', FALSE); |
| 1044 | |
| 1045 | // ... |
| 1046 | // Do something crazy with the SQL code... like add it to a cron script for |
| 1047 | // later execution or something... |
| 1048 | // ... |
| 1049 | |
| 1050 | $data = $this->db->get()->result_array(); |
| 1051 | |
| 1052 | // Would execute and return an array of results of the following query: |
Andrey Andreev | 896d3e3 | 2014-01-07 17:13:25 +0200 | [diff] [blame] | 1053 | // SELECT field1, field1 from mytable where field3 = 5; |
| 1054 | |
| 1055 | .. note:: Double calls to ``get_compiled_select()`` while you're using the |
| 1056 | Query Builder Caching functionality and NOT resetting your queries |
| 1057 | will results in the cache being merged twice. That in turn will |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1058 | i.e. if you're caching a ``select()`` - select the same field twice. |
| 1059 | |
| 1060 | *************** |
| 1061 | Class Reference |
| 1062 | *************** |
| 1063 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1064 | .. php:class:: CI_DB_query_builder |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1065 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1066 | .. php:method:: reset_query() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1067 | |
| 1068 | :returns: CI_DB_query_builder instance (method chaining) |
| 1069 | :rtype: CI_DB_query_builder |
| 1070 | |
| 1071 | Resets the current Query Builder state. Useful when you want |
| 1072 | to build a query that can be cancelled under certain conditions. |
| 1073 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1074 | .. php:method:: start_cache() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1075 | |
| 1076 | :returns: CI_DB_query_builder instance (method chaining) |
| 1077 | :rtype: CI_DB_query_builder |
| 1078 | |
| 1079 | Starts the Query Builder cache. |
| 1080 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1081 | .. php:method:: stop_cache() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1082 | |
| 1083 | :returns: CI_DB_query_builder instance (method chaining) |
| 1084 | :rtype: CI_DB_query_builder |
| 1085 | |
| 1086 | Stops the Query Builder cache. |
| 1087 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1088 | .. php:method:: flush_cache() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1089 | |
| 1090 | :returns: CI_DB_query_builder instance (method chaining) |
| 1091 | :rtype: CI_DB_query_builder |
| 1092 | |
| 1093 | Empties the Query Builder cache. |
| 1094 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1095 | .. php:method:: set_dbprefix([$prefix = '']) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1096 | |
| 1097 | :param string $prefix: The new prefix to use |
| 1098 | :returns: The DB prefix in use |
| 1099 | :rtype: string |
| 1100 | |
| 1101 | Sets the database prefix, without having to reconnect. |
| 1102 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1103 | .. php:method:: dbprefix([$table = '']) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1104 | |
| 1105 | :param string $table: The table name to prefix |
| 1106 | :returns: The prefixed table name |
| 1107 | :rtype: string |
| 1108 | |
| 1109 | Prepends a database prefix, if one exists in configuration. |
| 1110 | |
Andrey Andreev | ff806f9 | 2015-03-16 17:05:25 +0200 | [diff] [blame] | 1111 | .. php:method:: count_all_results([$table = '', [$reset = TRUE]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1112 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1113 | :param string $table: Table name |
Andrey Andreev | ff806f9 | 2015-03-16 17:05:25 +0200 | [diff] [blame] | 1114 | :param bool $reset: Whether to reset values for SELECTs |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1115 | :returns: Number of rows in the query result |
| 1116 | :rtype: int |
| 1117 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 1118 | Generates a platform-specific query string that counts |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1119 | all records returned by an Query Builder query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1120 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1121 | .. php:method:: get([$table = ''[, $limit = NULL[, $offset = NULL]]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1122 | |
| 1123 | :param string $table: The table to query |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1124 | :param int $limit: The LIMIT clause |
| 1125 | :param int $offset: The OFFSET clause |
| 1126 | :returns: CI_DB_result instance (method chaining) |
| 1127 | :rtype: CI_DB_result |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1128 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1129 | Compiles and runs SELECT statement based on the already |
| 1130 | called Query Builder methods. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1131 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1132 | .. php:method:: get_where([$table = ''[, $where = NULL[, $limit = NULL[, $offset = NULL]]]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1133 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1134 | :param mixed $table: The table(s) to fetch data from; string or array |
| 1135 | :param string $where: The WHERE clause |
| 1136 | :param int $limit: The LIMIT clause |
| 1137 | :param int $offset: The OFFSET clause |
| 1138 | :returns: CI_DB_result instance (method chaining) |
| 1139 | :rtype: CI_DB_result |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1140 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1141 | Same as ``get()``, but also allows the WHERE to be added directly. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1142 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1143 | .. php:method:: select([$select = '*'[, $escape = NULL]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1144 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1145 | :param string $select: The SELECT portion of a query |
| 1146 | :param bool $escape: Whether to escape values and identifiers |
| 1147 | :returns: CI_DB_query_builder instance (method chaining) |
| 1148 | :rtype: CI_DB_query_builder |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1149 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1150 | Adds a SELECT clause to a query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1151 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1152 | .. php:method:: select_avg([$select = ''[, $alias = '']]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1153 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1154 | :param string $select: Field to compute the average of |
| 1155 | :param string $alias: Alias for the resulting value name |
| 1156 | :returns: CI_DB_query_builder instance (method chaining) |
| 1157 | :rtype: CI_DB_query_builder |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1158 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1159 | Adds a SELECT AVG(field) clause to a query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1160 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1161 | .. php:method:: select_max([$select = ''[, $alias = '']]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1162 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1163 | :param string $select: Field to compute the maximum of |
| 1164 | :param string $alias: Alias for the resulting value name |
| 1165 | :returns: CI_DB_query_builder instance (method chaining) |
| 1166 | :rtype: CI_DB_query_builder |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1167 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1168 | Adds a SELECT MAX(field) clause to a query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1169 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1170 | .. php:method:: select_min([$select = ''[, $alias = '']]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1171 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1172 | :param string $select: Field to compute the minimum of |
| 1173 | :param string $alias: Alias for the resulting value name |
| 1174 | :returns: CI_DB_query_builder instance (method chaining) |
| 1175 | :rtype: CI_DB_query_builder |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1176 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1177 | Adds a SELECT MIN(field) clause to a query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1178 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1179 | .. php:method:: select_sum([$select = ''[, $alias = '']]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1180 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1181 | :param string $select: Field to compute the sum of |
| 1182 | :param string $alias: Alias for the resulting value name |
| 1183 | :returns: CI_DB_query_builder instance (method chaining) |
| 1184 | :rtype: CI_DB_query_builder |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1185 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1186 | Adds a SELECT SUM(field) clause to a query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1187 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1188 | .. php:method:: distinct([$val = TRUE]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1189 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1190 | :param bool $val: Desired value of the "distinct" flag |
| 1191 | :returns: CI_DB_query_builder instance (method chaining) |
| 1192 | :rtype: CI_DB_query_builder |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1193 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1194 | Sets a flag which tells the query builder to add |
| 1195 | a DISTINCT clause to the SELECT portion of the query. |
| 1196 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1197 | .. php:method:: from($from) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1198 | |
| 1199 | :param mixed $from: Table name(s); string or array |
| 1200 | :returns: CI_DB_query_builder instance (method chaining) |
| 1201 | :rtype: CI_DB_query_builder |
| 1202 | |
| 1203 | Specifies the FROM clause of a query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1204 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1205 | .. php:method:: join($table, $cond[, $type = ''[, $escape = NULL]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1206 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1207 | :param string $table: Table name to join |
| 1208 | :param string $cond: The JOIN ON condition |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1209 | :param string $type: The JOIN type |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1210 | :param bool $escape: Whether to escape values and identifiers |
| 1211 | :returns: CI_DB_query_builder instance (method chaining) |
| 1212 | :rtype: CI_DB_query_builder |
| 1213 | |
| 1214 | Adds a JOIN clause to a query. |
| 1215 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1216 | .. php:method:: where($key[, $value = NULL[, $escape = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1217 | |
| 1218 | :param mixed $key: Name of field to compare, or associative array |
| 1219 | :param mixed $value: If a single key, compared to this value |
Andrey Andreev | a42a01c | 2015-07-29 16:55:14 +0300 | [diff] [blame] | 1220 | :param bool $escape: Whether to escape values and identifiers |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1221 | :returns: DB_query_builder instance |
| 1222 | :rtype: object |
| 1223 | |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1224 | Generates the WHERE portion of the query. |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1225 | Separates multiple calls with 'AND'. |
| 1226 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1227 | .. php:method:: or_where($key[, $value = NULL[, $escape = NULL]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1228 | |
| 1229 | :param mixed $key: Name of field to compare, or associative array |
| 1230 | :param mixed $value: If a single key, compared to this value |
Andrey Andreev | a42a01c | 2015-07-29 16:55:14 +0300 | [diff] [blame] | 1231 | :param bool $escape: Whether to escape values and identifiers |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1232 | :returns: DB_query_builder instance |
| 1233 | :rtype: object |
| 1234 | |
| 1235 | Generates the WHERE portion of the query. |
| 1236 | Separates multiple calls with 'OR'. |
| 1237 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1238 | .. php:method:: or_where_in([$key = NULL[, $values = NULL[, $escape = NULL]]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1239 | |
| 1240 | :param string $key: The field to search |
| 1241 | :param array $values: The values searched on |
Andrey Andreev | a42a01c | 2015-07-29 16:55:14 +0300 | [diff] [blame] | 1242 | :param bool $escape: Whether to escape values and identifiers |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1243 | :returns: DB_query_builder instance |
| 1244 | :rtype: object |
| 1245 | |
| 1246 | Generates a WHERE field IN('item', 'item') SQL query, |
| 1247 | joined with 'OR' if appropriate. |
| 1248 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1249 | .. php:method:: or_where_not_in([$key = NULL[, $values = NULL[, $escape = NULL]]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1250 | |
| 1251 | :param string $key: The field to search |
| 1252 | :param array $values: The values searched on |
Andrey Andreev | a42a01c | 2015-07-29 16:55:14 +0300 | [diff] [blame] | 1253 | :param bool $escape: Whether to escape values and identifiers |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1254 | :returns: DB_query_builder instance |
| 1255 | :rtype: object |
| 1256 | |
| 1257 | Generates a WHERE field NOT IN('item', 'item') SQL query, |
| 1258 | joined with 'OR' if appropriate. |
| 1259 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1260 | .. php:method:: where_in([$key = NULL[, $values = NULL[, $escape = NULL]]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1261 | |
| 1262 | :param string $key: Name of field to examine |
| 1263 | :param array $values: Array of target values |
Andrey Andreev | a42a01c | 2015-07-29 16:55:14 +0300 | [diff] [blame] | 1264 | :param bool $escape: Whether to escape values and identifiers |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1265 | :returns: DB_query_builder instance |
| 1266 | :rtype: object |
| 1267 | |
| 1268 | Generates a WHERE field IN('item', 'item') SQL query, |
| 1269 | joined with 'AND' if appropriate. |
| 1270 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1271 | .. php:method:: where_not_in([$key = NULL[, $values = NULL[, $escape = NULL]]]) |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1272 | |
| 1273 | :param string $key: Name of field to examine |
| 1274 | :param array $values: Array of target values |
Andrey Andreev | a42a01c | 2015-07-29 16:55:14 +0300 | [diff] [blame] | 1275 | :param bool $escape: Whether to escape values and identifiers |
James L Parry | 7381711 | 2014-12-08 03:09:29 -0800 | [diff] [blame] | 1276 | :returns: DB_query_builder instance |
| 1277 | :rtype: object |
| 1278 | |
| 1279 | Generates a WHERE field NOT IN('item', 'item') SQL query, |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1280 | joined with 'AND' if appropriate. |
| 1281 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1282 | .. php:method:: group_start() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1283 | |
| 1284 | :returns: CI_DB_query_builder instance (method chaining) |
| 1285 | :rtype: CI_DB_query_builder |
| 1286 | |
| 1287 | Starts a group expression, using ANDs for the conditions inside it. |
| 1288 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1289 | .. php:method:: or_group_start() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1290 | |
| 1291 | :returns: CI_DB_query_builder instance (method chaining) |
| 1292 | :rtype: CI_DB_query_builder |
| 1293 | |
| 1294 | Starts a group expression, using ORs for the conditions inside it. |
| 1295 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1296 | .. php:method:: not_group_start() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1297 | |
| 1298 | :returns: CI_DB_query_builder instance (method chaining) |
| 1299 | :rtype: CI_DB_query_builder |
| 1300 | |
| 1301 | Starts a group expression, using AND NOTs for the conditions inside it. |
| 1302 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1303 | .. php:method:: or_not_group_start() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1304 | |
| 1305 | :returns: CI_DB_query_builder instance (method chaining) |
| 1306 | :rtype: CI_DB_query_builder |
| 1307 | |
| 1308 | Starts a group expression, using OR NOTs for the conditions inside it. |
| 1309 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1310 | .. php:method:: group_end() |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1311 | |
| 1312 | :returns: DB_query_builder instance |
| 1313 | :rtype: object |
| 1314 | |
| 1315 | Ends a group expression. |
| 1316 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1317 | .. php:method:: like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1318 | |
| 1319 | :param string $field: Field name |
| 1320 | :param string $match: Text portion to match |
| 1321 | :param string $side: Which side of the expression to put the '%' wildcard on |
| 1322 | :param bool $escape: Whether to escape values and identifiers |
| 1323 | :returns: CI_DB_query_builder instance (method chaining) |
| 1324 | :rtype: CI_DB_query_builder |
| 1325 | |
| 1326 | Adds a LIKE clause to a query, separating multiple calls with AND. |
| 1327 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1328 | .. php:method:: or_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1329 | |
| 1330 | :param string $field: Field name |
| 1331 | :param string $match: Text portion to match |
| 1332 | :param string $side: Which side of the expression to put the '%' wildcard on |
| 1333 | :param bool $escape: Whether to escape values and identifiers |
| 1334 | :returns: CI_DB_query_builder instance (method chaining) |
| 1335 | :rtype: CI_DB_query_builder |
| 1336 | |
| 1337 | Adds a LIKE clause to a query, separating multiple class with OR. |
| 1338 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1339 | .. php:method:: not_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1340 | |
| 1341 | :param string $field: Field name |
| 1342 | :param string $match: Text portion to match |
| 1343 | :param string $side: Which side of the expression to put the '%' wildcard on |
| 1344 | :param bool $escape: Whether to escape values and identifiers |
| 1345 | :returns: CI_DB_query_builder instance (method chaining) |
| 1346 | :rtype: CI_DB_query_builder |
| 1347 | |
| 1348 | Adds a NOT LIKE clause to a query, separating multiple calls with AND. |
| 1349 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1350 | .. php:method:: or_not_like($field[, $match = ''[, $side = 'both'[, $escape = NULL]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1351 | |
| 1352 | :param string $field: Field name |
| 1353 | :param string $match: Text portion to match |
| 1354 | :param string $side: Which side of the expression to put the '%' wildcard on |
| 1355 | :param bool $escape: Whether to escape values and identifiers |
| 1356 | :returns: CI_DB_query_builder instance (method chaining) |
| 1357 | :rtype: CI_DB_query_builder |
| 1358 | |
| 1359 | Adds a NOT LIKE clause to a query, separating multiple calls with OR. |
| 1360 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1361 | .. php:method:: having($key[, $value = NULL[, $escape = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1362 | |
| 1363 | :param mixed $key: Identifier (string) or associative array of field/value pairs |
| 1364 | :param string $value: Value sought if $key is an identifier |
| 1365 | :param string $escape: Whether to escape values and identifiers |
| 1366 | :returns: CI_DB_query_builder instance (method chaining) |
| 1367 | :rtype: CI_DB_query_builder |
| 1368 | |
| 1369 | Adds a HAVING clause to a query, separating multiple calls with AND. |
| 1370 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1371 | .. php:method:: or_having($key[, $value = NULL[, $escape = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1372 | |
| 1373 | :param mixed $key: Identifier (string) or associative array of field/value pairs |
| 1374 | :param string $value: Value sought if $key is an identifier |
| 1375 | :param string $escape: Whether to escape values and identifiers |
| 1376 | :returns: CI_DB_query_builder instance (method chaining) |
| 1377 | :rtype: CI_DB_query_builder |
| 1378 | |
| 1379 | Adds a HAVING clause to a query, separating multiple calls with OR. |
| 1380 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1381 | .. php:method:: group_by($by[, $escape = NULL]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1382 | |
| 1383 | :param mixed $by: Field(s) to group by; string or array |
| 1384 | :returns: CI_DB_query_builder instance (method chaining) |
| 1385 | :rtype: CI_DB_query_builder |
| 1386 | |
| 1387 | Adds a GROUP BY clause to a query. |
| 1388 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1389 | .. php:method:: order_by($orderby[, $direction = ''[, $escape = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1390 | |
| 1391 | :param string $orderby: Field to order by |
| 1392 | :param string $direction: The order requested - ASC, DESC or random |
| 1393 | :param bool $escape: Whether to escape values and identifiers |
| 1394 | :returns: CI_DB_query_builder instance (method chaining) |
| 1395 | :rtype: CI_DB_query_builder |
| 1396 | |
| 1397 | Adds an ORDER BY clause to a query. |
| 1398 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1399 | .. php:method:: limit($value[, $offset = 0]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1400 | |
| 1401 | :param int $value: Number of rows to limit the results to |
| 1402 | :param int $offset: Number of rows to skip |
| 1403 | :returns: CI_DB_query_builder instance (method chaining) |
| 1404 | :rtype: CI_DB_query_builder |
| 1405 | |
| 1406 | Adds LIMIT and OFFSET clauses to a query. |
| 1407 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1408 | .. php:method:: offset($offset) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1409 | |
| 1410 | :param int $offset: Number of rows to skip |
| 1411 | :returns: CI_DB_query_builder instance (method chaining) |
| 1412 | :rtype: CI_DB_query_builder |
| 1413 | |
| 1414 | Adds an OFFSET clause to a query. |
| 1415 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1416 | .. php:method:: set($key[, $value = ''[, $escape = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1417 | |
| 1418 | :param mixed $key: Field name, or an array of field/value pairs |
| 1419 | :param string $value: Field value, if $key is a single field |
| 1420 | :param bool $escape: Whether to escape values and identifiers |
| 1421 | :returns: CI_DB_query_builder instance (method chaining) |
| 1422 | :rtype: CI_DB_query_builder |
| 1423 | |
| 1424 | Adds field/value pairs to be passed later to ``insert()``, |
| 1425 | ``update()`` or ``replace()``. |
| 1426 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1427 | .. php:method:: insert([$table = ''[, $set = NULL[, $escape = NULL]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1428 | |
| 1429 | :param string $table: Table name |
| 1430 | :param array $set: An associative array of field/value pairs |
| 1431 | :param bool $escape: Whether to escape values and identifiers |
| 1432 | :returns: TRUE on success, FALSE on failure |
| 1433 | :rtype: bool |
| 1434 | |
| 1435 | Compiles and executes an INSERT statement. |
| 1436 | |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1437 | .. php:method:: insert_batch($table[, $set = NULL[, $escape = NULL[, $batch_size = 100]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1438 | |
| 1439 | :param string $table: Table name |
| 1440 | :param array $set: Data to insert |
| 1441 | :param bool $escape: Whether to escape values and identifiers |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1442 | :param int $batch_size: Count of rows to insert at once |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1443 | :returns: Number of rows inserted or FALSE on failure |
| 1444 | :rtype: mixed |
| 1445 | |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1446 | Compiles and executes batch ``INSERT`` statements. |
| 1447 | |
| 1448 | .. note:: When more than ``$batch_size`` rows are provided, multiple |
| 1449 | ``INSERT`` queries will be executed, each trying to insert |
| 1450 | up to ``$batch_size`` rows. |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1451 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1452 | .. php:method:: set_insert_batch($key[, $value = ''[, $escape = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1453 | |
| 1454 | :param mixed $key: Field name or an array of field/value pairs |
| 1455 | :param string $value: Field value, if $key is a single field |
| 1456 | :param bool $escape: Whether to escape values and identifiers |
| 1457 | :returns: CI_DB_query_builder instance (method chaining) |
| 1458 | :rtype: CI_DB_query_builder |
| 1459 | |
| 1460 | Adds field/value pairs to be inserted in a table later via ``insert_batch()``. |
| 1461 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1462 | .. php:method:: update([$table = ''[, $set = NULL[, $where = NULL[, $limit = NULL]]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1463 | |
| 1464 | :param string $table: Table name |
| 1465 | :param array $set: An associative array of field/value pairs |
| 1466 | :param string $where: The WHERE clause |
| 1467 | :param int $limit: The LIMIT clause |
| 1468 | :returns: TRUE on success, FALSE on failure |
| 1469 | :rtype: bool |
| 1470 | |
| 1471 | Compiles and executes an UPDATE statement. |
| 1472 | |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1473 | .. php:method:: update_batch($table[, $set = NULL[, $value = NULL[, $batch_size = 100]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1474 | |
| 1475 | :param string $table: Table name |
| 1476 | :param array $set: Field name, or an associative array of field/value pairs |
| 1477 | :param string $value: Field value, if $set is a single field |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1478 | :param int $batch_size: Count of conditions to group in a single query |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1479 | :returns: Number of rows updated or FALSE on failure |
| 1480 | :rtype: mixed |
| 1481 | |
Andrey Andreev | 105a48b | 2016-02-04 15:45:10 +0200 | [diff] [blame] | 1482 | Compiles and executes batch ``UPDATE`` statements. |
| 1483 | |
| 1484 | .. note:: When more than ``$batch_size`` field/value pairs are provided, |
| 1485 | multiple queries will be executed, each handling up to |
| 1486 | ``$batch_size`` field/value pairs. |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1487 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1488 | .. php:method:: set_update_batch($key[, $value = ''[, $escape = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1489 | |
| 1490 | :param mixed $key: Field name or an array of field/value pairs |
| 1491 | :param string $value: Field value, if $key is a single field |
| 1492 | :param bool $escape: Whether to escape values and identifiers |
| 1493 | :returns: CI_DB_query_builder instance (method chaining) |
| 1494 | :rtype: CI_DB_query_builder |
| 1495 | |
| 1496 | Adds field/value pairs to be updated in a table later via ``update_batch()``. |
| 1497 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1498 | .. php:method:: replace([$table = ''[, $set = NULL]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1499 | |
| 1500 | :param string $table: Table name |
| 1501 | :param array $set: An associative array of field/value pairs |
| 1502 | :returns: TRUE on success, FALSE on failure |
| 1503 | :rtype: bool |
| 1504 | |
| 1505 | Compiles and executes a REPLACE statement. |
| 1506 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1507 | .. php:method:: delete([$table = ''[, $where = ''[, $limit = NULL[, $reset_data = TRUE]]]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1508 | |
| 1509 | :param mixed $table: The table(s) to delete from; string or array |
| 1510 | :param string $where: The WHERE clause |
| 1511 | :param int $limit: The LIMIT clause |
| 1512 | :param bool $reset_data: TRUE to reset the query "write" clause |
| 1513 | :returns: CI_DB_query_builder instance (method chaining) or FALSE on failure |
| 1514 | :rtype: mixed |
| 1515 | |
| 1516 | Compiles and executes a DELETE query. |
| 1517 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1518 | .. php:method:: truncate([$table = '']) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1519 | |
| 1520 | :param string $table: Table name |
| 1521 | :returns: TRUE on success, FALSE on failure |
| 1522 | :rtype: bool |
| 1523 | |
| 1524 | Executes a TRUNCATE statement on a table. |
| 1525 | |
| 1526 | .. note:: If the database platform in use doesn't support TRUNCATE, |
| 1527 | a DELETE statement will be used instead. |
| 1528 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1529 | .. php:method:: empty_table([$table = '']) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1530 | |
| 1531 | :param string $table: Table name |
| 1532 | :returns: TRUE on success, FALSE on failure |
| 1533 | :rtype: bool |
| 1534 | |
| 1535 | Deletes all records from a table via a DELETE statement. |
| 1536 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1537 | .. php:method:: get_compiled_select([$table = ''[, $reset = TRUE]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1538 | |
| 1539 | :param string $table: Table name |
| 1540 | :param bool $reset: Whether to reset the current QB values or not |
| 1541 | :returns: The compiled SQL statement as a string |
| 1542 | :rtype: string |
| 1543 | |
| 1544 | Compiles a SELECT statement and returns it as a string. |
| 1545 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1546 | .. php:method:: get_compiled_insert([$table = ''[, $reset = TRUE]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1547 | |
| 1548 | :param string $table: Table name |
| 1549 | :param bool $reset: Whether to reset the current QB values or not |
| 1550 | :returns: The compiled SQL statement as a string |
| 1551 | :rtype: string |
| 1552 | |
| 1553 | Compiles an INSERT statement and returns it as a string. |
| 1554 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1555 | .. php:method:: get_compiled_update([$table = ''[, $reset = TRUE]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1556 | |
| 1557 | :param string $table: Table name |
| 1558 | :param bool $reset: Whether to reset the current QB values or not |
| 1559 | :returns: The compiled SQL statement as a string |
| 1560 | :rtype: string |
| 1561 | |
| 1562 | Compiles an UPDATE statement and returns it as a string. |
| 1563 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 1564 | .. php:method:: get_compiled_delete([$table = ''[, $reset = TRUE]]) |
Andrey Andreev | 4fce5c4 | 2014-12-11 17:11:48 +0200 | [diff] [blame] | 1565 | |
| 1566 | :param string $table: Table name |
| 1567 | :param bool $reset: Whether to reset the current QB values or not |
| 1568 | :returns: The compiled SQL statement as a string |
| 1569 | :rtype: string |
| 1570 | |
David Wosnitza | d31a4e6 | 2014-12-12 16:35:35 +0100 | [diff] [blame] | 1571 | Compiles a DELETE statement and returns it as a string. |