admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 1 | <!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 Allard | 404e35d | 2007-08-07 01:00:45 +0000 | [diff] [blame] | 5 | <title>CodeIgniter User Guide : Models</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 14 |
|
| 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 Allard | 3d879d5 | 2008-01-18 19:41:32 +0000 | [diff] [blame^] | 19 | <meta name='author' content='ExpressionEngine Dev Team' />
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 20 | <meta name='description' content='CodeIgniter User Guide' />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 21 |
|
| 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 Allard | 39b622d | 2008-01-16 21:10:09 +0000 | [diff] [blame] | 31 | <td><h1>CodeIgniter User Guide Version 1.6.0</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 32 | <td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 33 | </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 Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 43 | <a href="http://www.codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 44 | <a href="../index.html">User Guide Home</a> ›
|
| 45 | Models
|
| 46 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 47 | <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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 48 | </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 |
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 60 | <p>Models are <strong>optionally</strong> available for those who want to use a more traditional MVC approach.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 61 |
|
| 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 Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 68 | <li><a href="#auto_load_model">Auto-Loading a Model</a> </li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 69 | <li><a href="#conn">Connecting to your Database</a></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 70 | </ul>
|
| 71 |
|
| 72 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 73 |
|
| 74 | <h2><a name="what"></a>What is a Model?</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 75 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 76 | <p>Models are PHP classes that are designed to work with information in your database. For example, let's say
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 77 | you use CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 78 | retrieve your blog data. Here is an example of what such a model class might look like:</p>
|
| 79 |
|
| 80 | <code>
|
| 81 | class Blogmodel extends Model {<br />
|
| 82 | <br />
|
| 83 | var $title = '';<br />
|
| 84 | var $content = '';<br />
|
| 85 | var $date = '';<br />
|
| 86 | <br />
|
| 87 | function Blogmodel()<br />
|
| 88 | {<br />
|
| 89 | // Call the Model constructor<br />
|
| 90 | parent::Model();<br />
|
| 91 | }<br />
|
| 92 | <br />
|
| 93 | function get_last_ten_entries()<br />
|
| 94 | {<br />
|
| 95 | $query = $this->db->get('entries', 10);<br />
|
| 96 | return $query->result();<br />
|
| 97 | }<br />
|
| 98 | <br />
|
| 99 | function insert_entry()<br />
|
| 100 | {<br />
|
| 101 | $this->title = $_POST['title'];<br />
|
| 102 | $this->content = $_POST['content'];<br />
|
| 103 | $this->date = time();<br />
|
| 104 | <br />
|
| 105 | $this->db->insert('entries', $this);<br />
|
| 106 | }<br />
|
| 107 | <br />
|
| 108 | function update_entry()<br />
|
| 109 | {<br />
|
| 110 | $this->title = $_POST['title'];<br />
|
| 111 | $this->content = $_POST['content'];<br />
|
| 112 | $this->date = time();<br />
|
| 113 | <br />
|
| 114 | $this->db->update('entries', $this, array('id', $_POST['id']));<br />
|
| 115 | }<br />
|
| 116 | <br />
|
| 117 | }</code>
|
| 118 |
|
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 119 | <p>Note: The functions in the above example use the <a href="../database/active_record.html">Active Record</a> database functions.</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 120 |
|
| 121 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 122 |
|
| 123 | <h2><a name="anatomy"></a>Anatomy of a Model</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 124 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 125 | <p>Model classes are stored in your <dfn>application/models/</dfn> folder. They can be nested within sub-folders if you
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 126 | want this type of organization.</p>
|
| 127 |
|
| 128 | <p>The basic prototype for a model class is this:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 129 |
|
| 130 |
|
| 131 | <code>
|
| 132 | class <var>Model_name</var> extends Model {<br />
|
| 133 | <br />
|
| 134 | function <var>Model_name</var>()<br />
|
| 135 | {<br />
|
| 136 | parent::Model();<br />
|
| 137 | }<br />
|
| 138 | }</code>
|
| 139 |
|
Derek Jones | 57211eb | 2008-01-17 15:56:05 +0000 | [diff] [blame] | 140 | <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.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 141 | Make 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>
|
| 146 | class <var>User_model</var> extends Model {<br />
|
| 147 | <br />
|
| 148 | function <var>User_model</var>()<br />
|
| 149 | {<br />
|
| 150 | parent::Model();<br />
|
| 151 | }<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 Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 159 |
|
| 160 | <h2><a name="loading"></a>Loading a Model</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 161 |
|
| 162 | <p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
|
| 163 | To load a model you will use the following function:</p>
|
| 164 |
|
| 165 | <code>$this->load->model('<var>Model_name</var>');</code>
|
| 166 |
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 167 | <p>If you model is located in a sub-folder, include the relative path from your models folder. For example, if
|
| 168 | you 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 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 173 | <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
|
| 182 | function:</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>
|
| 194 | class Blog_controller extends Controller {<br />
|
| 195 | <br />
|
| 196 | function blog()<br />
|
| 197 | {<br />
|
| 198 | $this->load->model('Blog');<br />
|
| 199 | <br />
|
| 200 | $data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
|
| 201 | $this->load->view('blog', $data);<br />
|
| 202 | }<br />
|
| 203 | }</code>
|
| 204 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 205 | <h2><a name="auto_load_model" id="auto_load_model"></a>Auto-loading Models</h2>
|
Derek Allard | bafe651 | 2008-01-17 03:37:53 +0000 | [diff] [blame] | 206 | <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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 207 |
|
| 208 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 209 | <h2><a name="conn"></a>Connecting to your Database</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 210 |
|
| 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>
|
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 214 | <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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 215 | <li>You can tell the model loading function to auto-connect by passing <kbd>TRUE</kbd> (boolean) via the third parameter,
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 216 | and connectivity settings, as defined in your database config file will be used:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 217 |
|
| 218 | <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
|
| 219 | </li>
|
| 220 |
|
| 221 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 222 | <li>You can manually pass database connectivity settings via the third parameter:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 223 |
|
| 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 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 239 | </div>
|
| 240 | <!-- END CONTENT -->
|
| 241 |
|
| 242 |
|
| 243 | <div id="footer">
|
| 244 | <p>
|
| 245 | Previous Topic: <a href="views.html">Views</a>
|
| 246 | ·
|
| 247 | <a href="#top">Top of Page</a> ·
|
| 248 | <a href="../index.html">User Guide Home</a> ·
|
| 249 | Next Topic: <a href="helpers.html">Helpers</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 250 | </p>
|
Derek Allard | d2df9bc | 2007-04-15 17:41:17 +0000 | [diff] [blame] | 251 | <p><a href="http://www.codeigniter.com">CodeIgniter</a> · Copyright © 2007 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 252 | </div>
|
| 253 |
|
| 254 | </body>
|
| 255 | </html> |