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">
|
Derek Allard | afd99ac | 2008-01-19 19:59:14 +0000 | [diff] [blame] | 2 | <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 3 | <head>
|
| 4 |
|
Derek Allard | 8039d4c | 2008-05-31 02:47:56 +0000 | [diff] [blame] | 5 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
Derek Jones | fd93d22 | 2008-05-06 15:18:50 +0000 | [diff] [blame] | 6 | <title>Models : CodeIgniter User Guide</title>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 |
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 11 | <script type="text/javascript" src="../nav/nav.js"></script>
|
admin | 2296fc3 | 2006-09-27 21:07:02 +0000 | [diff] [blame] | 12 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
admin | 17a890d | 2006-09-27 20:42:42 +0000 | [diff] [blame] | 13 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
Derek Allard | b341237 | 2007-10-25 12:15:16 +0000 | [diff] [blame] | 14 | <script type="text/javascript" src="../nav/user_guide_menu.js"></script>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 15 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 | </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.jpg" width="153" height="44" 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 Jones | 1ca3fc4 | 2008-06-27 00:19:33 +0000 | [diff] [blame^] | 30 | <td><h1>CodeIgniter User Guide Version 1.6.3</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 31 | <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] | 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">
|
Derek Jones | 7a9193a | 2008-01-21 18:39:20 +0000 | [diff] [blame] | 42 | <a href="http://codeigniter.com/">CodeIgniter Home</a> ›
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 43 | <a href="../index.html">User Guide Home</a> ›
|
| 44 | Models
|
| 45 | </td>
|
Derek Allard | bc03091 | 2007-06-24 18:25:29 +0000 | [diff] [blame] | 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 <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] | 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 |
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 59 | <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] | 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>
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 67 | <li><a href="#auto_load_model">Auto-Loading a Model</a> </li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 68 | <li><a href="#conn">Connecting to your Database</a></li>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 69 | </ul>
|
| 70 |
|
| 71 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 72 |
|
| 73 | <h2><a name="what"></a>What is a Model?</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 74 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 75 | <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] | 76 | 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] | 77 | retrieve your blog data. Here is an example of what such a model class might look like:</p>
|
| 78 |
|
| 79 | <code>
|
| 80 | class Blogmodel extends Model {<br />
|
| 81 | <br />
|
| 82 | var $title = '';<br />
|
| 83 | var $content = '';<br />
|
| 84 | var $date = '';<br />
|
| 85 | <br />
|
| 86 | function Blogmodel()<br />
|
| 87 | {<br />
|
| 88 | // Call the Model constructor<br />
|
| 89 | parent::Model();<br />
|
| 90 | }<br />
|
| 91 | <br />
|
| 92 | function get_last_ten_entries()<br />
|
| 93 | {<br />
|
| 94 | $query = $this->db->get('entries', 10);<br />
|
| 95 | return $query->result();<br />
|
| 96 | }<br />
|
| 97 | <br />
|
| 98 | function insert_entry()<br />
|
| 99 | {<br />
|
Derek Allard | d379734 | 2008-05-07 11:25:19 +0000 | [diff] [blame] | 100 | $this->title = $_POST['title']; // please read the below note<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 101 | $this->content = $_POST['content'];<br />
|
| 102 | $this->date = time();<br />
|
| 103 | <br />
|
| 104 | $this->db->insert('entries', $this);<br />
|
| 105 | }<br />
|
| 106 | <br />
|
| 107 | function update_entry()<br />
|
| 108 | {<br />
|
| 109 | $this->title = $_POST['title'];<br />
|
| 110 | $this->content = $_POST['content'];<br />
|
| 111 | $this->date = time();<br />
|
| 112 | <br />
|
Derek Allard | 36a8395 | 2008-05-20 10:56:34 +0000 | [diff] [blame] | 113 | $this->db->update('entries', $this, array('id' => $_POST['id']));<br />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 114 | }<br />
|
| 115 | <br />
|
| 116 | }</code>
|
| 117 |
|
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 118 | <p>Note: The functions in the above example use the <a href="../database/active_record.html">Active Record</a> database functions.</p>
|
Derek Allard | 55bc1a8 | 2008-05-07 15:36:59 +0000 | [diff] [blame] | 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->input->post('title')</p>
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 120 | <h2><a name="anatomy"></a>Anatomy of a Model</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 121 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 122 | <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] | 123 | want this type of organization.</p>
|
| 124 |
|
| 125 | <p>The basic prototype for a model class is this:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 126 |
|
| 127 |
|
| 128 | <code>
|
| 129 | class <var>Model_name</var> extends Model {<br />
|
| 130 | <br />
|
| 131 | function <var>Model_name</var>()<br />
|
| 132 | {<br />
|
| 133 | parent::Model();<br />
|
| 134 | }<br />
|
| 135 | }</code>
|
| 136 |
|
Derek Jones | 57211eb | 2008-01-17 15:56:05 +0000 | [diff] [blame] | 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.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 138 | Make 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>
|
| 143 | class <var>User_model</var> extends Model {<br />
|
| 144 | <br />
|
| 145 | function <var>User_model</var>()<br />
|
| 146 | {<br />
|
| 147 | parent::Model();<br />
|
| 148 | }<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 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 156 |
|
| 157 | <h2><a name="loading"></a>Loading a Model</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 158 |
|
| 159 | <p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
|
| 160 | To load a model you will use the following function:</p>
|
| 161 |
|
| 162 | <code>$this->load->model('<var>Model_name</var>');</code>
|
| 163 |
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 164 | <p>If you model is located in a sub-folder, include the relative path from your models folder. For example, if
|
| 165 | you have a model located at <dfn>application/models/blog/queries.php</dfn> you'll load it using:</p>
|
| 166 |
|
Derek Allard | c8b2174 | 2008-05-18 03:57:40 +0000 | [diff] [blame] | 167 | <code>$this->load->model('<var>blog/queries</var>');</code>
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 168 |
|
| 169 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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
|
| 179 | function:</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>
|
| 191 | class Blog_controller extends Controller {<br />
|
| 192 | <br />
|
| 193 | function blog()<br />
|
| 194 | {<br />
|
| 195 | $this->load->model('Blog');<br />
|
| 196 | <br />
|
| 197 | $data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
|
| 198 | $this->load->view('blog', $data);<br />
|
| 199 | }<br />
|
| 200 | }</code>
|
| 201 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 202 | <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] | 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 204 |
|
| 205 |
|
Derek Allard | befdc87 | 2007-10-09 13:25:27 +0000 | [diff] [blame] | 206 | <h2><a name="conn"></a>Connecting to your Database</h2>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 212 | <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] | 213 | and connectivity settings, as defined in your database config file will be used:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 214 |
|
| 215 | <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
|
| 216 | </li>
|
| 217 |
|
| 218 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 219 | <li>You can manually pass database connectivity settings via the third parameter:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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 />
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 230 | <br />
|
| 231 | $this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
|
| 232 | </ul>
|
| 233 |
|
| 234 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 235 | </div>
|
| 236 | <!-- END CONTENT -->
|
| 237 |
|
| 238 |
|
| 239 | <div id="footer">
|
| 240 | <p>
|
| 241 | Previous Topic: <a href="views.html">Views</a>
|
| 242 | ·
|
| 243 | <a href="#top">Top of Page</a> ·
|
| 244 | <a href="../index.html">User Guide Home</a> ·
|
| 245 | Next Topic: <a href="helpers.html">Helpers</a>
|
Derek Allard | c644128 | 2007-07-04 23:54:32 +0000 | [diff] [blame] | 246 | </p>
|
Derek Jones | 0787043 | 2008-02-13 03:49:26 +0000 | [diff] [blame] | 247 | <p><a href="http://codeigniter.com">CodeIgniter</a> · Copyright © 2006-2008 · <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 248 | </div>
|
| 249 |
|
| 250 | </body>
|
| 251 | </html> |