blob: cf663428c5bc3cc1786c4448a762b02045349c91 [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html>
3<head>
4
Derek Allardd2df9bc2007-04-15 17:41:17 +00005<title>CodeIgniter User Guide</title>
adminb0dd10f2006-08-25 17:25:49 +00006
7<style type='text/css' media='all'>@import url('../userguide.css');</style>
8<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
9
admin17a890d2006-09-27 20:42:42 +000010<script type="text/javascript" src="../nav/nav.js"></script>
admin2296fc32006-09-27 21:07:02 +000011<script type="text/javascript" src="../nav/prototype.lite.js"></script>
admin17a890d2006-09-27 20:42:42 +000012<script type="text/javascript" src="../nav/moo.fx.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000013<script type="text/javascript">
14window.onload = function() {
admine334c472006-10-21 19:44:22 +000015 myHeight = new fx.Height('nav', {duration: 400});
adminb0dd10f2006-08-25 17:25:49 +000016 myHeight.hide();
17}
18</script>
19
20<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
21<meta http-equiv='expires' content='-1' />
22<meta http-equiv= 'pragma' content='no-cache' />
23<meta name='robots' content='all' />
24<meta name='author' content='Rick Ellis' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000025<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000026
27</head>
28<body>
29
30<!-- START NAVIGATION -->
31<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
32<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>
33<div id="masthead">
34<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
35<tr>
Derek Allardd2df9bc2007-04-15 17:41:17 +000036<td><h1>CodeIgniter User Guide Version 1.5.3</h1></td>
adminc0d5d522006-10-30 19:40:35 +000037<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000038</tr>
39</table>
40</div>
41<!-- END NAVIGATION -->
42
43
44<!-- START BREADCRUMB -->
45<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
46<tr>
47<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000048<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000049<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Models
51</td>
52<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
53</tr>
54</table>
55<!-- END BREADCRUMB -->
56
57<br clear="all" />
58
59
60<!-- START CONTENT -->
61<div id="content">
62
63<h1>Models</h1>
64
admin89ed1b32006-09-28 07:46:56 +000065<p>Models are <strong>optionally</strong> available for those who want to use a more traditional MVC approach.</p>
adminb0dd10f2006-08-25 17:25:49 +000066
67
68
69<ul>
70<li><a href="#what">What is a Model?</a></li>
71<li><a href="#anatomy">Anatomy of a Model</a></li>
72<li><a href="#loading">Loading a Model</a></li>
73<li><a href="#conn">Connecting to your Database</a></li>
74
75</ul>
76
77
78<a name="what"></a>
79<h2>What is a Model?</h2>
80
admine334c472006-10-21 19:44:22 +000081<p>Models are PHP classes that are designed to work with information in your database. For example, let's say
Derek Allardd2df9bc2007-04-15 17:41:17 +000082you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and
adminb0dd10f2006-08-25 17:25:49 +000083retrieve your blog data. Here is an example of what such a model class might look like:</p>
84
85<code>
86class&nbsp;Blogmodel&nbsp;extends&nbsp;Model&nbsp;{<br />
87<br />
88&nbsp;&nbsp;&nbsp;&nbsp;var $title&nbsp;&nbsp; = '';<br />
89&nbsp;&nbsp;&nbsp;&nbsp;var $content = '';<br />
90&nbsp;&nbsp;&nbsp;&nbsp;var $date&nbsp;&nbsp;&nbsp; = '';<br />
91<br />
92&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;Blogmodel()<br />
93&nbsp;&nbsp;&nbsp;&nbsp;{<br />
94&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Call the Model constructor<br />
95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
96&nbsp;&nbsp;&nbsp;&nbsp;}<br />
97&nbsp;&nbsp;&nbsp;&nbsp;<br />
98&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;get_last_ten_entries()<br />
99&nbsp;&nbsp;&nbsp;&nbsp;{<br />
100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$query = $this->db->get('entries', 10);<br />
101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $query->result();<br />
102&nbsp;&nbsp;&nbsp;&nbsp;}<br />
103<br />
104&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;insert_entry()<br />
105&nbsp;&nbsp;&nbsp;&nbsp;{<br />
106&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
107&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
108&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
109<br />
110&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->insert('entries',&nbsp;$this);<br />
111&nbsp;&nbsp;&nbsp;&nbsp;}<br />
112<br />
113&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;update_entry()<br />
114&nbsp;&nbsp;&nbsp;&nbsp;{<br />
115&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
116&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
118<br />
119&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->update('entries',&nbsp;$this, array('id',&nbsp;$_POST['id']));<br />
120&nbsp;&nbsp;&nbsp;&nbsp;}<br />
121<br />
122}</code>
123
admin58083462006-09-24 17:58:27 +0000124<p>Note: The functions in the above example use the <a href="../database/active_record.html">Active Record</a> database functions.</p>
adminb0dd10f2006-08-25 17:25:49 +0000125
126
127<a name="anatomy"></a>
128<h2>Anatomy of a Model</h2>
129
admine334c472006-10-21 19:44:22 +0000130<p>Model classes are stored in your <dfn>application/models/</dfn> folder. They can be nested within sub-folders if you
admin89ed1b32006-09-28 07:46:56 +0000131want this type of organization.</p>
132
133<p>The basic prototype for a model class is this:</p>
adminb0dd10f2006-08-25 17:25:49 +0000134
135
136<code>
137class&nbsp;<var>Model_name</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
138<br />
139&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>Model_name</var>()<br />
140&nbsp;&nbsp;&nbsp;&nbsp;{<br />
141&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
142&nbsp;&nbsp;&nbsp;&nbsp;}<br />
143}</code>
144
admine334c472006-10-21 19:44:22 +0000145<p>Where <var>Model_name</var> is the name of your class. Class names <strong>must</strong> be capitalized.
adminb0dd10f2006-08-25 17:25:49 +0000146Make sure your class extends the base Model class.</p>
147
148<p>The file name will be a lower case version of your class name. For example, if your class is this:</p>
149
150<code>
151class&nbsp;<var>User_model</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
152<br />
153&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>User_model</var>()<br />
154&nbsp;&nbsp;&nbsp;&nbsp;{<br />
155&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
156&nbsp;&nbsp;&nbsp;&nbsp;}<br />
157}</code>
158
159<p>Your file will be this:</p>
160
161<code>application/models/<var>user_model.php</var></code>
162
163
164<a name="loading"></a>
165<h2>Loading a Model</h2>
166
167<p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
168To load a model you will use the following function:</p>
169
170<code>$this->load->model('<var>Model_name</var>');</code>
171
admin89ed1b32006-09-28 07:46:56 +0000172<p>If you model is located in a sub-folder, include the relative path from your models folder. For example, if
173you have a model located at <dfn>application/models/blog/queries.php</dfn> you'll load it using:</p>
174
175<code>$this->load->model('blog/queries');</code>
176
177
adminb0dd10f2006-08-25 17:25:49 +0000178<p>Once loaded, you will access your model functions using an object with the same name as your class:</p>
179
180<code>
181$this->load->model('<var>Model_name</var>');<br />
182<br />
183$this-><var>Model_name</var>->function();
184</code>
185
186<p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
187function:</p>
188
189
190<code>
191$this->load->model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
192<br />
193$this-><kbd>fubar</kbd>->function();
194</code>
195
196<p>Here is an example of a controller, that loads a model, then serves a view:</p>
197
198<code>
199class&nbsp;Blog_controller&nbsp;extends&nbsp;Controller&nbsp;{<br />
200<br />
201&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;blog()<br />
202&nbsp;&nbsp;&nbsp;&nbsp;{<br />
203&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->model('Blog');<br />
204<br />
205&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
206&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('blog', $data);<br />
207&nbsp;&nbsp;&nbsp;&nbsp;}<br />
208}</code>
209
210
211
212<a name="conn"></a>
213<h2>Connecting to your Database</h2>
214
215<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>
216
217<ul>
admin58083462006-09-24 17:58:27 +0000218<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>
adminb0dd10f2006-08-25 17:25:49 +0000219<li>You can tell the model loading function to auto-connect by passing <kbd>TRUE</kbd> (boolean) via the third parameter,
admine334c472006-10-21 19:44:22 +0000220and connectivity settings, as defined in your database config file will be used:
adminb0dd10f2006-08-25 17:25:49 +0000221
222 <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
223 </li>
224
225
admine334c472006-10-21 19:44:22 +0000226<li>You can manually pass database connectivity settings via the third parameter:
adminb0dd10f2006-08-25 17:25:49 +0000227
228
229 <code>$config['hostname'] = "localhost";<br />
230 $config['username'] = "myusername";<br />
231 $config['password'] = "mypassword";<br />
232 $config['database'] = "mydatabase";<br />
233 $config['dbdriver'] = "mysql";<br />
234 $config['dbprefix'] = "";<br />
235 $config['pconnect'] = FALSE;<br />
236 $config['db_debug'] = TRUE;<br />
237 $config['active_r'] = TRUE;<br />
238 <br />
239 $this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
240</ul>
241
242
adminb0dd10f2006-08-25 17:25:49 +0000243</div>
244<!-- END CONTENT -->
245
246
247<div id="footer">
248<p>
249Previous Topic:&nbsp;&nbsp;<a href="views.html">Views</a>
250&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
251<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
252<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
253Next Topic:&nbsp;&nbsp;<a href="helpers.html">Helpers</a>
254<p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000255<p><a href="http://www.codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2007 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
adminb0dd10f2006-08-25 17:25:49 +0000256</div>
257
258</body>
259</html>