-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.html
222 lines (190 loc) · 8.12 KB
/
models.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Menukaarten-docs</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="assets/css/stylesheet.css" media="screen,print">
<link rel="stylesheet" href="assets/css/print.css" media="print">
<link rel="stylesheet" type="text/css" href="assets/css/shCore.css" media="screen,print">
<link rel="stylesheet" type="text/css" href="assets/css/shThemeDefault.css" media="screen,print">
<script type="text/javascript" src="assets/js/jquery.min.js"></script>
<script type="text/javascript" src="assets/js/SyntaxHighlighter.js"></script>
<script type="text/javascript" src="assets/js/build_menu.js"></script>
</head>
<body>
<div id="header-wrapper">
<div id="header">
<h1>Documentation SexyFramework</h1>
<span>Created by Vincent Bremer & Douwe de Haan</span>
</div>
</div>
<div id="container">
<div id="menu-wrapper">
<div id="menu">
<h1>Table of contents</h1>
<ul></ul>
</div>
</div>
<div id="content-wrapper">
<div id="content">
<!-- START CONTENT -->
<h1>Models</h1>
<h2>Find</h2>
<p>Models represent knowledge. A model could be a single object (rather uninteresting), or it could be some structure of objects.</p>
<p>There should be a one-to-one correspondence between the model and its parts on the one hand, and the represented world as perceived by the owner of the model on the other hand. <sup><a href="#source_1">[1]</a></sup></p>
<h3>Find a row by id</h3>
To the user with the id 232<br>
<pre class="brush: php">
$user->find(232);
</pre>
To the user with the id 2<br>
<pre class="brush: php">
$user->find(2);
</pre>
<h3>Find all rows</h3>
To find all users from the database:<br>
<pre class="brush: php">
$user->find('all');
</pre>
Or you can just type:<br>
<pre class="brush: php">
$user->find();
</pre>
<h3>Get just one result from the database</h3>
Get the first user<br>
<pre class="brush: php">
$user->find('one');
</pre>
<h2>Where</h2>
Get the first user who's firstname is 'Jan':<br>
<pre class="brush: php">
$user->where('firstname = ?', 'Jan')->find('one');
</pre>
Find all users who's firstname is 'Jan':<br>
<pre class="brush: php">
$user->where('firstname = ?', 'Jan')->find('all');
</pre>
Find all users who firstname is 'Jan', 'Piet', or 'Dirk'.<br>
<pre class="brush: php">
$user->where('firstname = ?', array('Jan', 'Piet', 'Dirk')->find('all');
</pre>
Find all users who's firstname is 'Jan', and who's lastname is 'de Boer'<br>
<pre class="brush: php">
$user->where('firstname = ? AND lastname = ?', array('Jan', 'de Boer'))->find('all');
</pre>
Find all users that got a Gmail address:<br>
<pre class="brush: php">
$user->where('email LIKE ?', '%gmail')->find('all');
</pre>
<h2>Selecting fields</h2>
By default, the model return all fields with *. Optionally you can specify with field you want to return bij using the select function. You can give it an array with the fields you want, example:<br>
<pre class="brush: php">
$user->select('firstname', 'age', 'hometown')->find('all');
</pre>
<h2>Sorting the results</h2>
To sort all users by lastname and then firstname, use:<br>
<pre class="brush: php">
$user->sort('lastname ASC, firstname ASC')->find();
</pre>
To sort the users by age, oldest first:<br>
<pre class="brush: php">
$user->sort('age DESC')->find('all');
</pre>
<h2>Limit the results</h2>
<p>To limit the number of results, you can specify a limit.</p>
Get the users 10 to 20:<br>
<pre class="brush: php">
$user->limit(10, 10)->find('all');
</pre>
Get the users 0 to 50:
<pre class="brush: php">
$user->limit(0, 50)->find('all');
</pre>
Get the users 50 to 75:
<pre class="brush: php">
$user->limit(50, 25)->find('all');
</pre>
<h2>Set a custom table</h2>
<p>
When creating a new model, the name of model should be the singularized name of the database table. For example; when you have created the model User_Model, the database table it tries to connect to is users (lowercase, and pluralized). It's also possible to set a different table name by setting the $_set_table class variable. When you have a profile model that needs to use the users table, you could to the following:
</p>
<pre class="brush: php">
class Profile_Model extends Base_Model {
protected $_set_table = 'users';
}
</pre>
<h2>Insert row</h2>
<p>The update row only accepts one parameter: $data, which should be an array with key/value pairs. The key should be the column name in the database, and the value will be inserted in that column.</p>
Insert a new user with some data:<br>
<pre class="brush: php">
$data = array(
'firstname' => 'John',
'lastname' => 'Doe',
'age' => '43'
);
$user->insert($data);
</pre>
And insert another user:<br>
<pre class="brush: php">
$data = array(
'firstname' => 'Nina',
'lastname' => 'Dobrev',
'age' => '24',
'hometown' => 'Toronto'
);
$user->insert($data);
</pre>
<h2>Update row</h2>
<p>To update and existing row, you can use the function update. This functions accepts three parameters; $data, $fields, $values. The $data variable should be an array which contain the update data (see the insert function above). The $fields variable could be an integer, in which case the row will be updated with that array. When the $fields variable contains an string, it will be handled as the where clause (see the where function above). The last $values variable could be a string, integer or array.</p>
Update the user with the id 2:<br>
<pre class="brush: php">
$data = array(
'firstname' => 'John',
'lastname' => 'Doe',
'age' => '43'
);
$user->update($data, 2);
</pre>
Update the users who's hometown is set to Sneek:<br>
<pre class="brush: php">
$data = array(
'country' => 'Netherlands',
'postalcode' => '8605'
);
$user->update($data, 'hometown = ?', 'Sneek');
</pre>
Update all users who life in the The Netherlands and have an email address that contains gmail:<br>
<pre class="brush: php">
$data = array(
'firstname' => 'Pietje'
);
$user->update($data, 'country = ? AND email LIKE ?', array('The Netherlands', '%gmail.com'));
</pre>
<h2>Data validation</h2>
<p>To be sure the data that gets stored in the database is data you want, there is a helper with validation methods. In the base model there is a method that communicates with the helper. The steps for validating your data are explained here.</p>
<p>first of all, check what type of data is going to be stored in the database and make sure the fieldnames in the database are the same as the values in your array of object that will be passed to the model. <br />
After that, go into the model that needs the validation and make an array like the following one.
</p>
<pre class="brush: php">
protected $_validation_rules = array(
'firstname' => array('not_empty', 'min_length:3', 'max_length:50'),
'lastname' => array('not_empty', 'min_length:5', 'max_length:60'),
'phone' => array('not_empty', 'is_numeric', 'min_length:10', 'max_length:10'),
'email' => array('not_empty', 'is_email'),
'country' => array('regex:[^0-9]*(land)\b')
);
</pre>
<p>The validation rules are pretty simple and most of the names of the rules explain what they do. Not_empty will check wether the field is empty or not. The min_length checks if the length of the value is longer than the number that is given behind the colon. Max_length does pretty much the same, but the length must not exceed the number given. Is_numeric and is_email both check for the type of the value. regex checks if the value of the field matches that of the one given. In the example above, it checks wether the value of country ends with land. Each of the result will be put in an array.</p>
<p class="note">
If you want to create more rules, please follow this pattern. If it is a validation to check the type of the data, do not use a colon. If you need to check the data with a value, like length or the value of an integer, use the colon.
</p>
<p class="footnote">
[1] - <a href="http://www.codinghorror.com/blog/2008/05/understanding-model-view-controller.html" id="source_1" target="_blank">Codinghorror - Understanding model-view-controller</a> - 5 May 2008<br>
</p>
<!-- END CONTENT -->
</div>
</div>
</div>
<script type="text/javascript" src="assets/js/SyntaxHighlighter_settings.js"></script>
</body>
</html>