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 |
|
| 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 |
|
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>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 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>
|
admin | 0518661 | 2006-10-28 03:28:01 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
|
admin | c0d5d52 | 2006-10-30 19:40:35 +0000 | [diff] [blame] | 37 | <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] | 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> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Models
|
| 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 <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>
|
| 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 |
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 65 | <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] | 66 |
|
| 67 |
|
| 68 |
|
| 69 | <ul>
|
| 70 | <li><a href="#what">What is a Model?</a></li>
|
| 71 | <li><a href="#anatomy">Anatomy of a Model</a></li>
|
| 72 | <li><a href="#loading">Loading a Model</a></li>
|
| 73 | <li><a href="#conn">Connecting to your Database</a></li>
|
| 74 |
|
| 75 | </ul>
|
| 76 |
|
| 77 |
|
| 78 | <a name="what"></a>
|
| 79 | <h2>What is a Model?</h2>
|
| 80 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 81 | <p>Models are PHP classes that are designed to work with information in your database. For example, let's say
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 82 | you use Code Igniter to manage a blog. You might have a model class that contains functions to insert, update, and
|
| 83 | retrieve your blog data. Here is an example of what such a model class might look like:</p>
|
| 84 |
|
| 85 | <code>
|
| 86 | class Blogmodel extends Model {<br />
|
| 87 | <br />
|
| 88 | var $title = '';<br />
|
| 89 | var $content = '';<br />
|
| 90 | var $date = '';<br />
|
| 91 | <br />
|
| 92 | function Blogmodel()<br />
|
| 93 | {<br />
|
| 94 | // Call the Model constructor<br />
|
| 95 | parent::Model();<br />
|
| 96 | }<br />
|
| 97 | <br />
|
| 98 | function get_last_ten_entries()<br />
|
| 99 | {<br />
|
| 100 | $query = $this->db->get('entries', 10);<br />
|
| 101 | return $query->result();<br />
|
| 102 | }<br />
|
| 103 | <br />
|
| 104 | function insert_entry()<br />
|
| 105 | {<br />
|
| 106 | $this->title = $_POST['title'];<br />
|
| 107 | $this->content = $_POST['content'];<br />
|
| 108 | $this->date = time();<br />
|
| 109 | <br />
|
| 110 | $this->db->insert('entries', $this);<br />
|
| 111 | }<br />
|
| 112 | <br />
|
| 113 | function update_entry()<br />
|
| 114 | {<br />
|
| 115 | $this->title = $_POST['title'];<br />
|
| 116 | $this->content = $_POST['content'];<br />
|
| 117 | $this->date = time();<br />
|
| 118 | <br />
|
| 119 | $this->db->update('entries', $this, array('id', $_POST['id']));<br />
|
| 120 | }<br />
|
| 121 | <br />
|
| 122 | }</code>
|
| 123 |
|
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 124 | <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] | 125 |
|
| 126 |
|
| 127 | <a name="anatomy"></a>
|
| 128 | <h2>Anatomy of a Model</h2>
|
| 129 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 130 | <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] | 131 | want this type of organization.</p>
|
| 132 |
|
| 133 | <p>The basic prototype for a model class is this:</p>
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 134 |
|
| 135 |
|
| 136 | <code>
|
| 137 | class <var>Model_name</var> extends Model {<br />
|
| 138 | <br />
|
| 139 | function <var>Model_name</var>()<br />
|
| 140 | {<br />
|
| 141 | parent::Model();<br />
|
| 142 | }<br />
|
| 143 | }</code>
|
| 144 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 145 | <p>Where <var>Model_name</var> is the name of your class. Class names <strong>must</strong> be capitalized.
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 146 | Make sure your class extends the base Model class.</p>
|
| 147 |
|
| 148 | <p>The file name will be a lower case version of your class name. For example, if your class is this:</p>
|
| 149 |
|
| 150 | <code>
|
| 151 | class <var>User_model</var> extends Model {<br />
|
| 152 | <br />
|
| 153 | function <var>User_model</var>()<br />
|
| 154 | {<br />
|
| 155 | parent::Model();<br />
|
| 156 | }<br />
|
| 157 | }</code>
|
| 158 |
|
| 159 | <p>Your file will be this:</p>
|
| 160 |
|
| 161 | <code>application/models/<var>user_model.php</var></code>
|
| 162 |
|
| 163 |
|
| 164 | <a name="loading"></a>
|
| 165 | <h2>Loading a Model</h2>
|
| 166 |
|
| 167 | <p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
|
| 168 | To load a model you will use the following function:</p>
|
| 169 |
|
| 170 | <code>$this->load->model('<var>Model_name</var>');</code>
|
| 171 |
|
admin | 89ed1b3 | 2006-09-28 07:46:56 +0000 | [diff] [blame] | 172 | <p>If you model is located in a sub-folder, include the relative path from your models folder. For example, if
|
| 173 | you have a model located at <dfn>application/models/blog/queries.php</dfn> you'll load it using:</p>
|
| 174 |
|
| 175 | <code>$this->load->model('blog/queries');</code>
|
| 176 |
|
| 177 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 178 | <p>Once loaded, you will access your model functions using an object with the same name as your class:</p>
|
| 179 |
|
| 180 | <code>
|
| 181 | $this->load->model('<var>Model_name</var>');<br />
|
| 182 | <br />
|
| 183 | $this-><var>Model_name</var>->function();
|
| 184 | </code>
|
| 185 |
|
| 186 | <p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
|
| 187 | function:</p>
|
| 188 |
|
| 189 |
|
| 190 | <code>
|
| 191 | $this->load->model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
|
| 192 | <br />
|
| 193 | $this-><kbd>fubar</kbd>->function();
|
| 194 | </code>
|
| 195 |
|
| 196 | <p>Here is an example of a controller, that loads a model, then serves a view:</p>
|
| 197 |
|
| 198 | <code>
|
| 199 | class Blog_controller extends Controller {<br />
|
| 200 | <br />
|
| 201 | function blog()<br />
|
| 202 | {<br />
|
| 203 | $this->load->model('Blog');<br />
|
| 204 | <br />
|
| 205 | $data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
|
| 206 | $this->load->view('blog', $data);<br />
|
| 207 | }<br />
|
| 208 | }</code>
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 | <a name="conn"></a>
|
| 213 | <h2>Connecting to your Database</h2>
|
| 214 |
|
| 215 | <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>
|
| 216 |
|
| 217 | <ul>
|
admin | 5808346 | 2006-09-24 17:58:27 +0000 | [diff] [blame] | 218 | <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] | 219 | <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] | 220 | and connectivity settings, as defined in your database config file will be used:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 221 |
|
| 222 | <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
|
| 223 | </li>
|
| 224 |
|
| 225 |
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 226 | <li>You can manually pass database connectivity settings via the third parameter:
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 227 |
|
| 228 |
|
| 229 | <code>$config['hostname'] = "localhost";<br />
|
| 230 | $config['username'] = "myusername";<br />
|
| 231 | $config['password'] = "mypassword";<br />
|
| 232 | $config['database'] = "mydatabase";<br />
|
| 233 | $config['dbdriver'] = "mysql";<br />
|
| 234 | $config['dbprefix'] = "";<br />
|
| 235 | $config['pconnect'] = FALSE;<br />
|
| 236 | $config['db_debug'] = TRUE;<br />
|
| 237 | $config['active_r'] = TRUE;<br />
|
| 238 | <br />
|
| 239 | $this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
|
| 240 | </ul>
|
| 241 |
|
| 242 |
|
admin | b0dd10f | 2006-08-25 17:25:49 +0000 | [diff] [blame] | 243 | </div>
|
| 244 | <!-- END CONTENT -->
|
| 245 |
|
| 246 |
|
| 247 | <div id="footer">
|
| 248 | <p>
|
| 249 | Previous Topic: <a href="views.html">Views</a>
|
| 250 | ·
|
| 251 | <a href="#top">Top of Page</a> ·
|
| 252 | <a href="../index.html">User Guide Home</a> ·
|
| 253 | Next Topic: <a href="helpers.html">Helpers</a>
|
| 254 | <p>
|
| 255 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 256 | </div>
|
| 257 |
|
| 258 | </body>
|
| 259 | </html> |