blob: 9a672ff2d08e0bddd97ffbed30f535ab43936fc5 [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">
Derek Allardafd99ac2008-01-19 19:59:14 +00002<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
adminb0dd10f2006-08-25 17:25:49 +00003<head>
4
Derek Allard404e35d2007-08-07 01:00:45 +00005<title>CodeIgniter User Guide : Models</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>
Derek Allardb3412372007-10-25 12:15:16 +000013<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
adminb0dd10f2006-08-25 17:25:49 +000014
15<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
Derek Allard3d879d52008-01-18 19:41:32 +000019<meta name='author' content='ExpressionEngine Dev Team' />
Derek Allardd2df9bc2007-04-15 17:41:17 +000020<meta name='description' content='CodeIgniter User Guide' />
adminb0dd10f2006-08-25 17:25:49 +000021
22</head>
23<body>
24
25<!-- START NAVIGATION -->
26<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
27<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>
28<div id="masthead">
29<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
30<tr>
Derek Allard39b622d2008-01-16 21:10:09 +000031<td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
adminc0d5d522006-10-30 19:40:35 +000032<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
adminb0dd10f2006-08-25 17:25:49 +000033</tr>
34</table>
35</div>
36<!-- END NAVIGATION -->
37
38
39<!-- START BREADCRUMB -->
40<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
41<tr>
42<td id="breadcrumb">
Derek Allardd2df9bc2007-04-15 17:41:17 +000043<a href="http://www.codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
adminb0dd10f2006-08-25 17:25:49 +000044<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
45Models
46</td>
Derek Allardbc030912007-06-24 18:25:29 +000047<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&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>
adminb0dd10f2006-08-25 17:25:49 +000048</tr>
49</table>
50<!-- END BREADCRUMB -->
51
52<br clear="all" />
53
54
55<!-- START CONTENT -->
56<div id="content">
57
58<h1>Models</h1>
59
admin89ed1b32006-09-28 07:46:56 +000060<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 +000061
62
63
64<ul>
65<li><a href="#what">What is a Model?</a></li>
66<li><a href="#anatomy">Anatomy of a Model</a></li>
67<li><a href="#loading">Loading a Model</a></li>
Derek Allardbefdc872007-10-09 13:25:27 +000068<li><a href="#auto_load_model">Auto-Loading a Model</a> </li>
adminb0dd10f2006-08-25 17:25:49 +000069<li><a href="#conn">Connecting to your Database</a></li>
adminb0dd10f2006-08-25 17:25:49 +000070</ul>
71
72
Derek Allardbefdc872007-10-09 13:25:27 +000073
74<h2><a name="what"></a>What is a Model?</h2>
adminb0dd10f2006-08-25 17:25:49 +000075
admine334c472006-10-21 19:44:22 +000076<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 +000077you 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 +000078retrieve your blog data. Here is an example of what such a model class might look like:</p>
79
80<code>
81class&nbsp;Blogmodel&nbsp;extends&nbsp;Model&nbsp;{<br />
82<br />
83&nbsp;&nbsp;&nbsp;&nbsp;var $title&nbsp;&nbsp; = '';<br />
84&nbsp;&nbsp;&nbsp;&nbsp;var $content = '';<br />
85&nbsp;&nbsp;&nbsp;&nbsp;var $date&nbsp;&nbsp;&nbsp; = '';<br />
86<br />
87&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;Blogmodel()<br />
88&nbsp;&nbsp;&nbsp;&nbsp;{<br />
89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Call the Model constructor<br />
90&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
91&nbsp;&nbsp;&nbsp;&nbsp;}<br />
92&nbsp;&nbsp;&nbsp;&nbsp;<br />
93&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;get_last_ten_entries()<br />
94&nbsp;&nbsp;&nbsp;&nbsp;{<br />
95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$query = $this->db->get('entries', 10);<br />
96&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $query->result();<br />
97&nbsp;&nbsp;&nbsp;&nbsp;}<br />
98<br />
99&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;insert_entry()<br />
100&nbsp;&nbsp;&nbsp;&nbsp;{<br />
101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
102&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
103&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
104<br />
105&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->insert('entries',&nbsp;$this);<br />
106&nbsp;&nbsp;&nbsp;&nbsp;}<br />
107<br />
108&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;update_entry()<br />
109&nbsp;&nbsp;&nbsp;&nbsp;{<br />
110&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
112&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
113<br />
114&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->update('entries',&nbsp;$this, array('id',&nbsp;$_POST['id']));<br />
115&nbsp;&nbsp;&nbsp;&nbsp;}<br />
116<br />
117}</code>
118
admin58083462006-09-24 17:58:27 +0000119<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 +0000120
121
Derek Allardbefdc872007-10-09 13:25:27 +0000122
123<h2><a name="anatomy"></a>Anatomy of a Model</h2>
adminb0dd10f2006-08-25 17:25:49 +0000124
admine334c472006-10-21 19:44:22 +0000125<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 +0000126want this type of organization.</p>
127
128<p>The basic prototype for a model class is this:</p>
adminb0dd10f2006-08-25 17:25:49 +0000129
130
131<code>
132class&nbsp;<var>Model_name</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
133<br />
134&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>Model_name</var>()<br />
135&nbsp;&nbsp;&nbsp;&nbsp;{<br />
136&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
137&nbsp;&nbsp;&nbsp;&nbsp;}<br />
138}</code>
139
Derek Jones57211eb2008-01-17 15:56:05 +0000140<p>Where <var>Model_name</var> is the name of your class. Class names <strong>must</strong> have the first letter capitalized with the rest of the name lowercase.
adminb0dd10f2006-08-25 17:25:49 +0000141Make sure your class extends the base Model class.</p>
142
143<p>The file name will be a lower case version of your class name. For example, if your class is this:</p>
144
145<code>
146class&nbsp;<var>User_model</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
147<br />
148&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>User_model</var>()<br />
149&nbsp;&nbsp;&nbsp;&nbsp;{<br />
150&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
151&nbsp;&nbsp;&nbsp;&nbsp;}<br />
152}</code>
153
154<p>Your file will be this:</p>
155
156<code>application/models/<var>user_model.php</var></code>
157
158
Derek Allardbefdc872007-10-09 13:25:27 +0000159
160<h2><a name="loading"></a>Loading a Model</h2>
adminb0dd10f2006-08-25 17:25:49 +0000161
162<p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
163To load a model you will use the following function:</p>
164
165<code>$this->load->model('<var>Model_name</var>');</code>
166
admin89ed1b32006-09-28 07:46:56 +0000167<p>If you model is located in a sub-folder, include the relative path from your models folder. For example, if
168you have a model located at <dfn>application/models/blog/queries.php</dfn> you'll load it using:</p>
169
170<code>$this->load->model('blog/queries');</code>
171
172
adminb0dd10f2006-08-25 17:25:49 +0000173<p>Once loaded, you will access your model functions using an object with the same name as your class:</p>
174
175<code>
176$this->load->model('<var>Model_name</var>');<br />
177<br />
178$this-><var>Model_name</var>->function();
179</code>
180
181<p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
182function:</p>
183
184
185<code>
186$this->load->model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
187<br />
188$this-><kbd>fubar</kbd>->function();
189</code>
190
191<p>Here is an example of a controller, that loads a model, then serves a view:</p>
192
193<code>
194class&nbsp;Blog_controller&nbsp;extends&nbsp;Controller&nbsp;{<br />
195<br />
196&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;blog()<br />
197&nbsp;&nbsp;&nbsp;&nbsp;{<br />
198&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->model('Blog');<br />
199<br />
200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
201&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('blog', $data);<br />
202&nbsp;&nbsp;&nbsp;&nbsp;}<br />
203}</code>
204
Derek Allardbefdc872007-10-09 13:25:27 +0000205<h2><a name="auto_load_model" id="auto_load_model"></a>Auto-loading Models</h2>
Derek Allardbafe6512008-01-17 03:37:53 +0000206<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 model to the autoload array.</p>
adminb0dd10f2006-08-25 17:25:49 +0000207
208
Derek Allardbefdc872007-10-09 13:25:27 +0000209<h2><a name="conn"></a>Connecting to your Database</h2>
adminb0dd10f2006-08-25 17:25:49 +0000210
211<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>
212
213<ul>
admin58083462006-09-24 17:58:27 +0000214<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 +0000215<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 +0000216and connectivity settings, as defined in your database config file will be used:
adminb0dd10f2006-08-25 17:25:49 +0000217
218 <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
219 </li>
220
221
admine334c472006-10-21 19:44:22 +0000222<li>You can manually pass database connectivity settings via the third parameter:
adminb0dd10f2006-08-25 17:25:49 +0000223
224
225 <code>$config['hostname'] = "localhost";<br />
226 $config['username'] = "myusername";<br />
227 $config['password'] = "mypassword";<br />
228 $config['database'] = "mydatabase";<br />
229 $config['dbdriver'] = "mysql";<br />
230 $config['dbprefix'] = "";<br />
231 $config['pconnect'] = FALSE;<br />
232 $config['db_debug'] = TRUE;<br />
233 $config['active_r'] = TRUE;<br />
234 <br />
235 $this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
236</ul>
237
238
adminb0dd10f2006-08-25 17:25:49 +0000239</div>
240<!-- END CONTENT -->
241
242
243<div id="footer">
244<p>
245Previous Topic:&nbsp;&nbsp;<a href="views.html">Views</a>
246&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
247<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
248<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
249Next Topic:&nbsp;&nbsp;<a href="helpers.html">Helpers</a>
Derek Allardc6441282007-07-04 23:54:32 +0000250</p>
Derek Allardd2df9bc2007-04-15 17:41:17 +0000251<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 +0000252</div>
253
254</body>
255</html>