blob: 26b85f67b67e759fc91019da44731a4ae88393a1 [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
5<title>Code Igniter User Guide</title>
6
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
10<script type="text/javascript" src="../scripts/nav.js"></script>
11<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
12<script type="text/javascript" src="../scripts/moo.fx.js"></script>
13<script type="text/javascript">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
16 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' />
25<meta name='description' content='Code Igniter User Guide' />
26
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>
admin9fc347d2006-09-21 00:00:28 +000036<td><h1>Code Igniter User Guide Version 1.4.1</h1></td>
adminb0dd10f2006-08-25 17:25:49 +000037<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
38</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">
48<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
49<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
65<p>Models are <strong>optionally</strong> available for those who want to use a more
66traditional MVC approach.</p>
67
68
69
70<ul>
71<li><a href="#what">What is a Model?</a></li>
72<li><a href="#anatomy">Anatomy of a Model</a></li>
73<li><a href="#loading">Loading a Model</a></li>
74<li><a href="#conn">Connecting to your Database</a></li>
75
76</ul>
77
78
79<a name="what"></a>
80<h2>What is a Model?</h2>
81
82<p>Models are PHP classes that are designed to work with information in your database. For example, let's say
83you use Code Igniter to manage a blog. You might have a model class that contains functions to insert, update, and
84retrieve your blog data. Here is an example of what such a model class might look like:</p>
85
86<code>
87class&nbsp;Blogmodel&nbsp;extends&nbsp;Model&nbsp;{<br />
88<br />
89&nbsp;&nbsp;&nbsp;&nbsp;var $title&nbsp;&nbsp; = '';<br />
90&nbsp;&nbsp;&nbsp;&nbsp;var $content = '';<br />
91&nbsp;&nbsp;&nbsp;&nbsp;var $date&nbsp;&nbsp;&nbsp; = '';<br />
92<br />
93&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;Blogmodel()<br />
94&nbsp;&nbsp;&nbsp;&nbsp;{<br />
95&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Call the Model constructor<br />
96&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
97&nbsp;&nbsp;&nbsp;&nbsp;}<br />
98&nbsp;&nbsp;&nbsp;&nbsp;<br />
99&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;get_last_ten_entries()<br />
100&nbsp;&nbsp;&nbsp;&nbsp;{<br />
101&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$query = $this->db->get('entries', 10);<br />
102&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $query->result();<br />
103&nbsp;&nbsp;&nbsp;&nbsp;}<br />
104<br />
105&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;insert_entry()<br />
106&nbsp;&nbsp;&nbsp;&nbsp;{<br />
107&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
108&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
109&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
110<br />
111&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->insert('entries',&nbsp;$this);<br />
112&nbsp;&nbsp;&nbsp;&nbsp;}<br />
113<br />
114&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;update_entry()<br />
115&nbsp;&nbsp;&nbsp;&nbsp;{<br />
116&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
117&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
118&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
119<br />
120&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->update('entries',&nbsp;$this, array('id',&nbsp;$_POST['id']));<br />
121&nbsp;&nbsp;&nbsp;&nbsp;}<br />
122<br />
123}</code>
124
125<p>Note: The functions in the above example use the <a href="../libraries/database/active_record.html">Active Record</a> database functions.</p>
126
127
128<a name="anatomy"></a>
129<h2>Anatomy of a Model</h2>
130
131<p>Model classes are stored in your <dfn>application/models/</dfn> folder. The basic prototype for a model is this:</p>
132
133
134<code>
135class&nbsp;<var>Model_name</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
136<br />
137&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>Model_name</var>()<br />
138&nbsp;&nbsp;&nbsp;&nbsp;{<br />
139&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
140&nbsp;&nbsp;&nbsp;&nbsp;}<br />
141}</code>
142
143<p>Where <var>Model_name</var> is the name of your class. Class names <strong>must</strong> be capitalized.
144Make sure your class extends the base Model class.</p>
145
146<p>The file name will be a lower case version of your class name. For example, if your class is this:</p>
147
148<code>
149class&nbsp;<var>User_model</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
150<br />
151&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>User_model</var>()<br />
152&nbsp;&nbsp;&nbsp;&nbsp;{<br />
153&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
154&nbsp;&nbsp;&nbsp;&nbsp;}<br />
155}</code>
156
157<p>Your file will be this:</p>
158
159<code>application/models/<var>user_model.php</var></code>
160
161
162<a name="loading"></a>
163<h2>Loading a Model</h2>
164
165<p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
166To load a model you will use the following function:</p>
167
168<code>$this->load->model('<var>Model_name</var>');</code>
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
203
204<a name="conn"></a>
205<h2>Connecting to your Database</h2>
206
207<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>
208
209<ul>
210<li>You can connect using the standard database methods <a href="../libraries/database/connecting.html">described here</a>, either from within your Controller class or your Model class.</li>
211<li>You can tell the model loading function to auto-connect by passing <kbd>TRUE</kbd> (boolean) via the third parameter,
212and connectivity settings, as defined in your database config file will be used:
213
214 <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
215 </li>
216
217
218<li>You can manually pass database connectivity settings via the third parameter:
219
220
221 <code>$config['hostname'] = "localhost";<br />
222 $config['username'] = "myusername";<br />
223 $config['password'] = "mypassword";<br />
224 $config['database'] = "mydatabase";<br />
225 $config['dbdriver'] = "mysql";<br />
226 $config['dbprefix'] = "";<br />
227 $config['pconnect'] = FALSE;<br />
228 $config['db_debug'] = TRUE;<br />
229 $config['active_r'] = TRUE;<br />
230 <br />
231 $this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
232</ul>
233
234
235
236
237
238
239
240</div>
241<!-- END CONTENT -->
242
243
244<div id="footer">
245<p>
246Previous Topic:&nbsp;&nbsp;<a href="views.html">Views</a>
247&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
248<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
249<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
250Next Topic:&nbsp;&nbsp;<a href="helpers.html">Helpers</a>
251<p>
252<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
253</div>
254
255</body>
256</html>