-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage.html
More file actions
378 lines (304 loc) · 12.5 KB
/
usage.html
File metadata and controls
378 lines (304 loc) · 12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Database - Usage - Fuel Documentation</title>
<link href="../../assets/css/main.css" media="screen" rel="stylesheet" />
<script type="text/javascript" src="../../assets/js/jquery-1.4.4.min.js"></script>
<script type="text/javascript" src="../../assets/js/nav.js"></script>
<script type="text/javascript" src="../../assets/js/highlight.pack.js"></script>
<script type="text/javascript">
$(function() {
show_nav('classes', '../../');
});
hljs.tabReplace = ' ';
hljs.initHighlightingOnLoad();
</script>
</head>
<body>
<header>
<h1>Fuel Documentation</h1>
</header>
<div id="main-nav"></div>
<section id="content">
<h2>Database Usage</h2>
<p>
Normal database interactions are to go through the <a href="./db.html">DB class</a>.
The following examples will give you a feel for how to go about using databases in Fuel.
</p>
<p>Database usage is devided into a couple of segments:</p>
<ul>
<li><a href="#running-queries">Running queries</a></li>
<li><a href="#selecting">Selecting</a>,</li>
<li><a href="#results">Results</a>,</li>
<li><a href="#filtering">Filtering</a> (where, order by, limit, offset),</li>
<li><a href="#inserting">Inserting</a>,</li>
<li><a href="#updating">Updating</a>,</li>
<li><a href="#deleting">Deleting</a> data</li>
<li><a href="#joins">Joins</a></li>
<li><a href="#escaping">Escaping</a></li>
</ul>
<article>
<h2 id="running-queries">Running queries</h2>
<p>First we prepare a query using <a href="./db.html#method_query">DB::query</a>.</p>
<pre class="php"><code>// returns a new Database_Query
$query = DB::query('SELECT * FROM `users`');
</code></pre>
<p>Now we can execute that query:</p>
<pre class="php"><code>$query = DB::query('SELECT * FROM `users`');
// return a new Database_MySQLi_Result
$query->execute();
// Or execute is on a different database group
$query->execute('another_group');
// or
$query->execute(Database_Connection::instance('another_group'));
// And we can chain then like this:
$result = DB::query('SELECT * FROM `users`')->execute();
</code></pre>
</article>
<article>
<h2 id="selecting">Selection data</h2>
<p>
First let's select data using <a href="./db.html#method_query">DB::query</a>.
As we are going to fetch a result from this query, we need to let the it know
what kind of query we are going to run.
</p>
<pre class="php"><code>
$result = DB::query('SELECT * FROM `users`', DB::SELECT)->execute();
</code></pre>
<p>
We can also select data using <a href="./db.html#method_select">DB::select</a> or
<a href="./db.html#method_select_array">DB::select_array</a>.
</p>
<pre class="php"><code>// Will execute SELECT `id`, `name` FROM `users`
$result = DB::select('id','name')->from('users')->execute();
</code></pre>
<p>If you want to alias columns, use arrays instead of strings</p>
<pre class="php"><code>// Will execute SELECT `name` as `the_name` FROM `users`;
$result = DB::select(array('name','the_name'))->from('users')->execute();
</code></pre>
<p>To select distinct values set distinct to <em>true</em>:</p>
<pre class="php"><code>// SELECT DISTINCT `name` FROM `users`
$users = DB::select('name')->from('users')->distinct(true)->execute();
print_r($users->as_array());
/*
Array
(
[0] => Array
(
[name] => Dirk
)
[1] => Array
(
[name] => John
)
)
*/
</code></pre>
<h4 id="results">Results</h4>
<p>
Executing a select query will generate a result object containing the requested database records.
By default the result is fetched as associative arrays. Here is an exaple how to influence this
behaviour.
</p>
<pre class="php"><code>// Will fetch the result as an associative array.
$result = DB::select('id','name')->from('users')->as_assoc()->execute();
// Will fetch the result as an object.
$result = DB::select('id','name')->from('users')->as_object()->execute();
// Will fetch the result as an Model_Users object.
$result = DB::select()->from('users')->as_object('Model_Users')->execute();
</code></pre>
<p>Want to know how many records you have fetched? It's dead simple!</p>
<pre class="php"><code>
$result = DB::select('*')->from('users')->execute();
// Just count the results, it returns an int.
$num_rows = count($result);
</code></pre>
<p>To access these results you eighter loop through the result object directly, or get the result array.</p>
<pre class="php"><code>$result = DB::select()->from('users')->execute();
foreach($result as $item)
{
// do something with $item
}
$result_array = $result->as_array();
foreach($result_array as $item)
{
// do something with $item
}
</code></pre>
<p>Optionaly we specify the array key and value to be returned from <em>as_array</em></p>
<pre class="php"><code>$result = DB::select()->from('users')->execute();
$on_key = $result->as_array('id');
foreach($on_key as $id => $item)
{
// $id will contain the records id
// do something with $item or it's $id
}
$key_value = $result->as_array('id', 'email');
foreach($key_value as $id => $email)
{
// now $email will be the email field.
// so you can do something with $id or $email
}
</code></pre>
</article>
<article>
<h2 id="filtering">Filtering</h2>
<h4 id="where_statements">Where statements</h4>
<p>
In order to set the conditions on our queries we can set WHERE conditions.
These examples also apply to updating and deleting.
</p>
<pre class="php"><code>// Will execute SELECT * FROM `users` WHERE `id` = 1
$result = DB::select()->from('users')->where('id', 1)->execute();
</code></pre>
<p>To influence the operator supply it like so:</p>
<pre class="php"><code>// Will execute SELECT * FROM `users` WHERE `id` = 1
$result = DB::select()->from('users')->where('id', '=' 1)->execute();
// Will execute SELECT * FROM `users` WHERE `id` IN (1, 2, 3)
$id_array = array(1,2,3);
$result = DB::select()->from('users')->where('id', 'in', $id_array)->execute();
// Will execute SELECT * FROM `users` WHERE `id` BETWEEN 1 AND 2
$result = DB::select()->from('users')->where('id', 'between', array(1, 2))->execute();
// Will execute SELECT * FROM `users` WHERE `id` != 1
$result = DB::select()->from('users')->where('id', '!=', 1)->execute();
// Will execute SELECT * FROM `users` WHERE `name` LIKE "john%"
$who = "john%";
$result = DB::select()->from('users')->where('id', 'like', $who)->execute();
</code></pre>
<p id="grouped_where">Grouped where statements are also supported:</p>
<pre class="php"><code>// SELECT * FROM `users` WHERE (`name` = 'John' AND `email` = 'john@example.com')
// OR (`name` = 'mike' OR `name` = 'dirk')
$result = DB::select()->from('users')->where_open()
->where('name', 'John')
->and_where('email', 'john@example.com')
->where_close()
->or_where_open()
->where('name', 'mike')
->or_where('name', 'dirk')
->or_where_close()->execute();
</code></pre>
<p id="where_between_in">The <em>BETWEEN</em> and <em>IN</em> also go through the <em>where</em> method:</p>
<pre class="php"><code>// SELECT * FROM `users` WHERE `id` BETWEEN 1 AND 10
$users = DB::select()->from('users')->where('id', 'between', array(1, 10))->execute();
// SELECT * FROM `users` WHERE `name` IN ('john', 'simon', 'dirk')
$users = DB::select()->from('users')->where('name', 'in', array('john', 'simon', 'dirk'))->execute();
</code></pre>
<h4 id="order_by">Order by statement</h4>
<p>For sorting data we use the order_by function.</p>
<pre class="php"><code>//SELECT * FROM `users` ORDER BY `name` ASC
DB::select()->from('users')->order_by('name','asc');
// SELECT * FROM `users` ORDER BY `name` ASC, `surname` DESC
DB::select()->from('users')->order_by('name','asc')->order_by('surname', 'desc');
// You can ommit the direction by leaving the second parameter out.
// SELECT * FROM `users` ORDER BY `name`
DB::select()->from('users')->order_by('name');
</code></pre>
<h4 id="limit_offset">Limit and offset</h4>
<p>For limiting the number of rows fetched we use the limit and offset function. Note that the offset function is only available when selecting data.</p>
<pre class="php"><code></code>// SELECT * FROM `users` LIMIT 1
DB::select()->from('users')->limit(1);
// SELECT * FROM `users` LIMIT 10 OFFSET 5
DB::select()->from('users')->limit(10)->offset(5);
// SELECT * FROM `users` ORDER BY `id` ASC LIMIT 10
DB::select('users')->order_by('id','asc')->limit(10);
</pre>
</article>
<article>
<h2 id="updating">Updating</h2>
<p>
For updating data we use <a href="./db.html#method_update">DB::update</a>.
If successfully executed an update query will return an integer representing
the amount of affected rows.
</p>
<p>To update a single column.</p>
<pre class="php"><code>// Will execute UPDATE `users` SET `name` = "John Random" WHERE `id` = "2";
$result = DB::update('users')
->value("name", "John Random")
->where('id', '=', '2')
->execute();
</code></pre>
<p>To update multiple columns.</p>
<pre class="php"><code>// Will execute UPDATE `users`
// SET `group` = "Peter Griffon", `email` = "peter@thehindenpeter.com"
// WHERE `id` = "16";
$result = DB::update('users')
->set(array(
'name' => "Peter Griffon",
'email' => "peter@thehindenpeter.com"
))
->where('id', '=', '16')
->execute();
</code></pre>
</article>
<article>
<h2 id="inserting">Inserting</h2>
<p>
For inserting data we use <a href="./db.html#method_insert">DB::insert</a>.
If succesfully executed an insert query will return an array containing a
list of insert id and rows created.
</p>
<pre class="php"><code>// Will execute INSERT INTO `users`(`name`,`email`,`password`)
// VALUES ("John Random", "john@example.com", "s0_s3cr3t")
list($insert_id, $rows_affected) = DB::insert('users')->set(array(
'name' => 'John Random',
'email' => 'john@example.com',
'password' => 's0_s3cr3t',
))->execute();
</code></pre>
<p>You can also set the columns and values seperatly</p>
<pre class="php"><code>// Will execute INSERT INTO `users`(`name`,`email`,`password`)
// VALUES ("John Random", "john@example.com", "s0_s3cr3t")
list($insert_id, $rows_affected) = DB::insert('users')->columns(array(
'name', 'email', 'password'
))->values(array(
'John Random', 'john@example.com', 's0_s3cr3t'
))->execute();
</code></pre>
</article>
<article>
<h2 id="deleting">Deleting</h2>
<p>
To delete records, use <a href="./db.html#method_delete">DB::delete</a>.
When executed it will return the number of rows affected.
</p>
<pre class="php"><code>// Empty the whole users table
$result = DB::delete('users')->execute(); // (int) 20
// Executes DELETE FROM `users` WHERE `email` LIKE "%@example.com"
$result = DB::delete('users')->where('email', 'like', '%@example.com')->execute(); // (int) 7
</code></pre>
</article>
<article>
<h2 id="joins">Joins</h2>
<p>When selecting data, you can also join other tables into the result.</p>
<pre class="php"><code>// Will execute SELECT * FROM `users` LEFT JOIN `roles` ON `roles`.`id` = `users`.`role_id`
$result = DB::select()->from('users')->join('roles','LEFT')->on('roles.id', '=', 'users.role_id');
// Will execute SELECT * FROM `users` RIGHT OUTER JOIN `roles` ON `roles`.`id` = `users`.`role_id`
$result = DB::select()->from('users')->join('roles','right outer')->on('roles.id', '=', 'users.role_id');
</code></pre>
</article>
<article>
<h2 id="escaping">Escaping</h2>
<p>Fields and values in database calls are escaped by default. In some cases you'll want to not escape data. The DB class provides a method to create <em>database expressions</em>, <a href="./db.html#method_expr">DB::expr</a>. If you don't want a value to get escaped, just wrap it in a database expression.</p>
<p>Database expressions are especially helpful when dealing with thing like MySQL's native function (like <em>COUNT</em>) and predefined constands (like <em>DEFAULT</em>).</p>
<pre class="php"><code>// Set a field to it's default
DB::update('users')->where('id', '=', 1)->set(array(
'some_column' => DB::expr('DEFAULT'),
))->execute();
// SELECT COUNT(*) FROM `users`
$result = DB::select(DB::expr('COUNT(*) as count'))->from('users')->execute();
// Get the current/first result
$result_arr = $result->current();
// Get the number of rows
$count = $result_arr['count'];
</code></pre>
</article>
</section>
<section id="footer">
<p>
<a href="http://fuelphp.com">Fuel</a> is released under the MIT license.<br />
© 2010 - 2011 Fuel Development Team
</p>
</section>
</body>
</html>