blob: 00ef9c1f4f281aa3da537179303517bac9b6b10d [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
3<head>
4
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6<title>Models : CodeIgniter User Guide</title>
7
8<style type='text/css' media='all'>@import url('../userguide.css');</style>
9<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
10
11<script type="text/javascript" src="../nav/nav.js"></script>
12<script type="text/javascript" src="../nav/prototype.lite.js"></script>
13<script type="text/javascript" src="../nav/moo.fx.js"></script>
14<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
15
16<meta http-equiv='expires' content='-1' />
17<meta http-equiv= 'pragma' content='no-cache' />
18<meta name='robots' content='all' />
19<meta name='author' content='ExpressionEngine Dev Team' />
20<meta name='description' content='CodeIgniter User Guide' />
21</head>
22<body>
23
24<!-- START NAVIGATION -->
25<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
26<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle_darker.jpg" width="154" height="43" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
27<div id="masthead">
28<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
29<tr>
Derek Jones733310d2009-02-11 01:13:43 +000030<td><h1>CodeIgniter User Guide Version 1.7.1</h1></td>
Derek Allard2067d1a2008-11-13 22:59:24 +000031<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
32</tr>
33</table>
34</div>
35<!-- END NAVIGATION -->
36
37
38<!-- START BREADCRUMB -->
39<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
40<tr>
41<td id="breadcrumb">
42<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
43<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
44Models
45</td>
46<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>
47</tr>
48</table>
49<!-- END BREADCRUMB -->
50
51<br clear="all" />
52
53
54<!-- START CONTENT -->
55<div id="content">
56
57<h1>Models</h1>
58
59<p>Models are <strong>optionally</strong> available for those who want to use a more traditional MVC approach.</p>
60
61
62
63<ul>
64<li><a href="#what">What is a Model?</a></li>
65<li><a href="#anatomy">Anatomy of a Model</a></li>
66<li><a href="#loading">Loading a Model</a></li>
67<li><a href="#auto_load_model">Auto-Loading a Model</a> </li>
68<li><a href="#conn">Connecting to your Database</a></li>
69</ul>
70
71
72
73<h2><a name="what"></a>What is a Model?</h2>
74
75<p>Models are PHP classes that are designed to work with information in your database. For example, let's say
76you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and
77retrieve your blog data. Here is an example of what such a model class might look like:</p>
78
79<code>
80class&nbsp;Blogmodel&nbsp;extends&nbsp;Model&nbsp;{<br />
81<br />
82&nbsp;&nbsp;&nbsp;&nbsp;var $title&nbsp;&nbsp; = '';<br />
83&nbsp;&nbsp;&nbsp;&nbsp;var $content = '';<br />
84&nbsp;&nbsp;&nbsp;&nbsp;var $date&nbsp;&nbsp;&nbsp; = '';<br />
85<br />
86&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;Blogmodel()<br />
87&nbsp;&nbsp;&nbsp;&nbsp;{<br />
88&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Call the Model constructor<br />
89&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
90&nbsp;&nbsp;&nbsp;&nbsp;}<br />
91&nbsp;&nbsp;&nbsp;&nbsp;<br />
92&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;get_last_ten_entries()<br />
93&nbsp;&nbsp;&nbsp;&nbsp;{<br />
94&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$query = $this->db->get('entries', 10);<br />
95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $query->result();<br />
96&nbsp;&nbsp;&nbsp;&nbsp;}<br />
97<br />
98&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;insert_entry()<br />
99&nbsp;&nbsp;&nbsp;&nbsp;{<br />
100&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title']; // please read the below note<br />
101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
102&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
103<br />
104&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->insert('entries',&nbsp;$this);<br />
105&nbsp;&nbsp;&nbsp;&nbsp;}<br />
106<br />
107&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;update_entry()<br />
108&nbsp;&nbsp;&nbsp;&nbsp;{<br />
109&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
110&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
112<br />
113&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->update('entries',&nbsp;$this, array('id' => $_POST['id']));<br />
114&nbsp;&nbsp;&nbsp;&nbsp;}<br />
115<br />
116}</code>
117
118<p>Note: The functions in the above example use the <a href="../database/active_record.html">Active Record</a> database functions.</p>
119<p class="important"><strong>Note:</strong> For the sake of simplicity in this example we're using $_POST directly. This is generally bad practice, and a more common approach would be to use the <a href="../libraries/input.html">Input Class</a> $this-&gt;input-&gt;post('title')</p>
120<h2><a name="anatomy"></a>Anatomy of a Model</h2>
121
122<p>Model classes are stored in your <dfn>application/models/</dfn> folder. They can be nested within sub-folders if you
123want this type of organization.</p>
124
125<p>The basic prototype for a model class is this:</p>
126
127
128<code>
129class&nbsp;<var>Model_name</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
130<br />
131&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>Model_name</var>()<br />
132&nbsp;&nbsp;&nbsp;&nbsp;{<br />
133&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
134&nbsp;&nbsp;&nbsp;&nbsp;}<br />
135}</code>
136
137<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.
138Make sure your class extends the base Model class.</p>
139
140<p>The file name will be a lower case version of your class name. For example, if your class is this:</p>
141
142<code>
143class&nbsp;<var>User_model</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
144<br />
145&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>User_model</var>()<br />
146&nbsp;&nbsp;&nbsp;&nbsp;{<br />
147&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
148&nbsp;&nbsp;&nbsp;&nbsp;}<br />
149}</code>
150
151<p>Your file will be this:</p>
152
153<code>application/models/<var>user_model.php</var></code>
154
155
156
157<h2><a name="loading"></a>Loading a Model</h2>
158
159<p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
160To load a model you will use the following function:</p>
161
162<code>$this->load->model('<var>Model_name</var>');</code>
163
164<p>If you model is located in a sub-folder, include the relative path from your models folder. For example, if
165you have a model located at <dfn>application/models/blog/queries.php</dfn> you'll load it using:</p>
166
167<code>$this->load->model('<var>blog/queries</var>');</code>
168
169
170<p>Once loaded, you will access your model functions using an object with the same name as your class:</p>
171
172<code>
173$this->load->model('<var>Model_name</var>');<br />
174<br />
175$this-><var>Model_name</var>->function();
176</code>
177
178<p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
179function:</p>
180
181
182<code>
183$this->load->model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
184<br />
185$this-><kbd>fubar</kbd>->function();
186</code>
187
188<p>Here is an example of a controller, that loads a model, then serves a view:</p>
189
190<code>
191class&nbsp;Blog_controller&nbsp;extends&nbsp;Controller&nbsp;{<br />
192<br />
193&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;blog()<br />
194&nbsp;&nbsp;&nbsp;&nbsp;{<br />
195&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->model('Blog');<br />
196<br />
197&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
198&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('blog', $data);<br />
199&nbsp;&nbsp;&nbsp;&nbsp;}<br />
200}</code>
201
202<h2><a name="auto_load_model" id="auto_load_model"></a>Auto-loading Models</h2>
203<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>
204
205
206<h2><a name="conn"></a>Connecting to your Database</h2>
207
208<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>
209
210<ul>
211<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>
212<li>You can tell the model loading function to auto-connect by passing <kbd>TRUE</kbd> (boolean) via the third parameter,
213and connectivity settings, as defined in your database config file will be used:
214
215 <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
216 </li>
217
218
219<li>You can manually pass database connectivity settings via the third parameter:
220
221
222 <code>$config['hostname'] = "localhost";<br />
223 $config['username'] = "myusername";<br />
224 $config['password'] = "mypassword";<br />
225 $config['database'] = "mydatabase";<br />
226 $config['dbdriver'] = "mysql";<br />
227 $config['dbprefix'] = "";<br />
228 $config['pconnect'] = FALSE;<br />
229 $config['db_debug'] = TRUE;<br />
230 <br />
231 $this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
232</ul>
233
234
235</div>
236<!-- END CONTENT -->
237
238
239<div id="footer">
240<p>
241Previous Topic:&nbsp;&nbsp;<a href="views.html">Views</a>
242&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
243<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
244<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
245Next Topic:&nbsp;&nbsp;<a href="helpers.html">Helpers</a>
246</p>
247<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
248</div>
249
250</body>
adminb0dd10f2006-08-25 17:25:49 +0000251</html>