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
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>CodeIgniter User Guide : Models</title>
<style type='text/css' media='all'>@import url('../userguide.css');</style>
<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
<script type="text/javascript" src="../nav/nav.js"></script>
<script type="text/javascript" src="../nav/prototype.lite.js"></script>
<script type="text/javascript" src="../nav/moo.fx.js"></script>
<script type="text/javascript">
window.onload = function() {
myHeight = new fx.Height('nav', {duration: 400});
myHeight.hide();
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv='expires' content='-1' />
<meta http-equiv= 'pragma' content='no-cache' />
<meta name='robots' content='all' />
<meta name='author' content='Rick Ellis' />
<meta name='description' content='CodeIgniter User Guide' />
</head>
<body>
<!-- START NAVIGATION -->
<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
<div id="masthead">
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td><h1>CodeIgniter User Guide Version 1.5.4</h1></td>
<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
</tr>
</table>
</div>
<!-- END NAVIGATION -->
<!-- START BREADCRUMB -->
<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
<tr>
<td id="breadcrumb">
<a href="http://www.codeigniter.com/">CodeIgniter Home</a> ›
<a href="../index.html">User Guide Home</a> ›
Models
</td>
<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
</tr>
</table>
<!-- END BREADCRUMB -->
<br clear="all" />
<!-- START CONTENT -->
<div id="content">
<h1>Models</h1>
<p>Models are <strong>optionally</strong> available for those who want to use a more traditional MVC approach.</p>
<ul>
<li><a href="#what">What is a Model?</a></li>
<li><a href="#anatomy">Anatomy of a Model</a></li>
<li><a href="#loading">Loading a Model</a></li>
<li><a href="#auto_load_model">Auto-Loading a Model</a> </li>
<li><a href="#conn">Connecting to your Database</a></li>
</ul>
<h2><a name="what"></a>What is a Model?</h2>
<p>Models are PHP classes that are designed to work with information in your database. For example, let's say
you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and
retrieve your blog data. Here is an example of what such a model class might look like:</p>
<code>
class Blogmodel extends Model {<br />
<br />
var $title = '';<br />
var $content = '';<br />
var $date = '';<br />
<br />
function Blogmodel()<br />
{<br />
// Call the Model constructor<br />
parent::Model();<br />
}<br />
<br />
function get_last_ten_entries()<br />
{<br />
$query = $this->db->get('entries', 10);<br />
return $query->result();<br />
}<br />
<br />
function insert_entry()<br />
{<br />
$this->title = $_POST['title'];<br />
$this->content = $_POST['content'];<br />
$this->date = time();<br />
<br />
$this->db->insert('entries', $this);<br />
}<br />
<br />
function update_entry()<br />
{<br />
$this->title = $_POST['title'];<br />
$this->content = $_POST['content'];<br />
$this->date = time();<br />
<br />
$this->db->update('entries', $this, array('id', $_POST['id']));<br />
}<br />
<br />
}</code>
<p>Note: The functions in the above example use the <a href="../database/active_record.html">Active Record</a> database functions.</p>
<h2><a name="anatomy"></a>Anatomy of a Model</h2>
<p>Model classes are stored in your <dfn>application/models/</dfn> folder. They can be nested within sub-folders if you
want this type of organization.</p>
<p>The basic prototype for a model class is this:</p>
<code>
class <var>Model_name</var> extends Model {<br />
<br />
function <var>Model_name</var>()<br />
{<br />
parent::Model();<br />
}<br />
}</code>
<p>Where <var>Model_name</var> is the name of your class. Class names <strong>must</strong> be capitalized.
Make sure your class extends the base Model class.</p>
<p>The file name will be a lower case version of your class name. For example, if your class is this:</p>
<code>
class <var>User_model</var> extends Model {<br />
<br />
function <var>User_model</var>()<br />
{<br />
parent::Model();<br />
}<br />
}</code>
<p>Your file will be this:</p>
<code>application/models/<var>user_model.php</var></code>
<h2><a name="loading"></a>Loading a Model</h2>
<p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
To load a model you will use the following function:</p>
<code>$this->load->model('<var>Model_name</var>');</code>
<p>If you model is located in a sub-folder, include the relative path from your models folder. For example, if
you have a model located at <dfn>application/models/blog/queries.php</dfn> you'll load it using:</p>
<code>$this->load->model('blog/queries');</code>
<p>Once loaded, you will access your model functions using an object with the same name as your class:</p>
<code>
$this->load->model('<var>Model_name</var>');<br />
<br />
$this-><var>Model_name</var>->function();
</code>
<p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
function:</p>
<code>
$this->load->model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
<br />
$this-><kbd>fubar</kbd>->function();
</code>
<p>Here is an example of a controller, that loads a model, then serves a view:</p>
<code>
class Blog_controller extends Controller {<br />
<br />
function blog()<br />
{<br />
$this->load->model('Blog');<br />
<br />
$data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
$this->load->view('blog', $data);<br />
}<br />
}</code>
<h2><a name="auto_load_model" id="auto_load_model"></a>Auto-loading Models</h2>
<p>If you find that you need a particular model globally throughout your application, you can tell CodeIgniter to auto-load it during system initialization. This is done by opening the application/config/autoload.php file and adding the mdoel to the autoload array.</p>
<h2><a name="conn"></a>Connecting to your Database</h2>
<p>When a model is loaded it does <strong>NOT</strong> connect automatically to your database. The following options for connecting are available to you:</p>
<ul>
<li>You can connect using the standard database methods <a href="../database/connecting.html">described here</a>, either from within your Controller class or your Model class.</li>
<li>You can tell the model loading function to auto-connect by passing <kbd>TRUE</kbd> (boolean) via the third parameter,
and connectivity settings, as defined in your database config file will be used:
<code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
</li>
<li>You can manually pass database connectivity settings via the third parameter:
<code>$config['hostname'] = "localhost";<br />
$config['username'] = "myusername";<br />
$config['password'] = "mypassword";<br />
$config['database'] = "mydatabase";<br />
$config['dbdriver'] = "mysql";<br />
$config['dbprefix'] = "";<br />
$config['pconnect'] = FALSE;<br />
$config['db_debug'] = TRUE;<br />
$config['active_r'] = TRUE;<br />
<br />
$this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
</ul>
</div>
<!-- END CONTENT -->
<div id="footer">
<p>
Previous Topic: <a href="views.html">Views</a>
·
<a href="#top">Top of Page</a> ·
<a href="../index.html">User Guide Home</a> ·
Next Topic: <a href="helpers.html">Helpers</a>
</p>
<p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
</div>
</body>
</html>
|