summaryrefslogtreecommitdiffstats
path: root/user_guide/general/models.html
diff options
context:
space:
mode:
Diffstat (limited to 'user_guide/general/models.html')
-rw-r--r--user_guide/general/models.html256
1 files changed, 256 insertions, 0 deletions
diff --git a/user_guide/general/models.html b/user_guide/general/models.html
new file mode 100644
index 000000000..dfb45d622
--- /dev/null
+++ b/user_guide/general/models.html
@@ -0,0 +1,256 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html>
+<head>
+
+<title>Code Igniter User Guide</title>
+
+<style type='text/css' media='all'>@import url('../userguide.css');</style>
+<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
+
+<script type="text/javascript" src="../scripts/nav.js"></script>
+<script type="text/javascript" src="../scripts/prototype.lite.js"></script>
+<script type="text/javascript" src="../scripts/moo.fx.js"></script>
+<script type="text/javascript">
+window.onload = function() {
+ myHeight = new fx.Height('nav', {duration: 400});
+ myHeight.hide();
+}
+</script>
+
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<meta http-equiv='expires' content='-1' />
+<meta http-equiv= 'pragma' content='no-cache' />
+<meta name='robots' content='all' />
+<meta name='author' content='Rick Ellis' />
+<meta name='description' content='Code Igniter User Guide' />
+
+</head>
+<body>
+
+<!-- START NAVIGATION -->
+<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
+<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>
+<div id="masthead">
+<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
+<tr>
+<td><h1>Code Igniter User Guide Version 1.4.0</h1></td>
+<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
+</tr>
+</table>
+</div>
+<!-- END NAVIGATION -->
+
+
+<!-- START BREADCRUMB -->
+<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
+<tr>
+<td id="breadcrumb">
+<a href="http://www.codeigniter.com/">Code Igniter Home</a> &nbsp;&#8250;&nbsp;
+<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
+Models
+</td>
+<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>
+</tr>
+</table>
+<!-- END BREADCRUMB -->
+
+<br clear="all" />
+
+
+<!-- START CONTENT -->
+<div id="content">
+
+<h1>Models</h1>
+
+<p>Models are <strong>optionally</strong> available for those who want to use a more
+traditional MVC approach.</p>
+
+
+
+<ul>
+<li><a href="#what">What is a Model?</a></li>
+<li><a href="#anatomy">Anatomy of a Model</a></li>
+<li><a href="#loading">Loading a Model</a></li>
+<li><a href="#conn">Connecting to your Database</a></li>
+
+</ul>
+
+
+<a name="what"></a>
+<h2>What is a Model?</h2>
+
+<p>Models are PHP classes that are designed to work with information in your database. For example, let's say
+you use Code Igniter to manage a blog. You might have a model class that contains functions to insert, update, and
+retrieve your blog data. Here is an example of what such a model class might look like:</p>
+
+<code>
+class&nbsp;Blogmodel&nbsp;extends&nbsp;Model&nbsp;{<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;var $title&nbsp;&nbsp; = '';<br />
+&nbsp;&nbsp;&nbsp;&nbsp;var $content = '';<br />
+&nbsp;&nbsp;&nbsp;&nbsp;var $date&nbsp;&nbsp;&nbsp; = '';<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;Blogmodel()<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Call the Model constructor<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
+&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+&nbsp;&nbsp;&nbsp;&nbsp;<br />
+&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;get_last_ten_entries()<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$query = $this->db->get('entries', 10);<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $query->result();<br />
+&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;insert_entry()<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->insert('entries',&nbsp;$this);<br />
+&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;update_entry()<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->title&nbsp;&nbsp; = $_POST['title'];<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->content = $_POST['content'];<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->date&nbsp;&nbsp;&nbsp; = time();<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->db->update('entries',&nbsp;$this, array('id',&nbsp;$_POST['id']));<br />
+&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+<br />
+}</code>
+
+<p>Note: The functions in the above example use the <a href="../libraries/database/active_record.html">Active Record</a> database functions.</p>
+
+
+<a name="anatomy"></a>
+<h2>Anatomy of a Model</h2>
+
+<p>Model classes are stored in your <dfn>application/models/</dfn> folder. The basic prototype for a model is this:</p>
+
+
+<code>
+class&nbsp;<var>Model_name</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>Model_name</var>()<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
+&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+}</code>
+
+<p>Where <var>Model_name</var> is the name of your class. Class names <strong>must</strong> be capitalized.
+Make sure your class extends the base Model class.</p>
+
+<p>The file name will be a lower case version of your class name. For example, if your class is this:</p>
+
+<code>
+class&nbsp;<var>User_model</var>&nbsp;extends&nbsp;Model&nbsp;{<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;<var>User_model</var>()<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parent::Model();<br />
+&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+}</code>
+
+<p>Your file will be this:</p>
+
+<code>application/models/<var>user_model.php</var></code>
+
+
+<a name="loading"></a>
+<h2>Loading a Model</h2>
+
+<p>Your models will typically be loaded and called from within your <a href="controllers.html">controller</a> functions.
+To load a model you will use the following function:</p>
+
+<code>$this->load->model('<var>Model_name</var>');</code>
+
+<p>Once loaded, you will access your model functions using an object with the same name as your class:</p>
+
+<code>
+$this->load->model('<var>Model_name</var>');<br />
+<br />
+$this-><var>Model_name</var>->function();
+</code>
+
+<p>If you would like your model assigned to a different object name you can specify it via the second parameter of the loading
+function:</p>
+
+
+<code>
+$this->load->model('<var>Model_name</var>', '<kbd>fubar</kbd>');<br />
+<br />
+$this-><kbd>fubar</kbd>->function();
+</code>
+
+<p>Here is an example of a controller, that loads a model, then serves a view:</p>
+
+<code>
+class&nbsp;Blog_controller&nbsp;extends&nbsp;Controller&nbsp;{<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;function&nbsp;blog()<br />
+&nbsp;&nbsp;&nbsp;&nbsp;{<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->model('Blog');<br />
+<br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$data['query'] = $this->Blog->get_last_ten_entries();<br /><br />
+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$this->load->view('blog', $data);<br />
+&nbsp;&nbsp;&nbsp;&nbsp;}<br />
+}</code>
+
+
+
+<a name="conn"></a>
+<h2>Connecting to your Database</h2>
+
+<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>
+
+<ul>
+<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>
+<li>You can tell the model loading function to auto-connect by passing <kbd>TRUE</kbd> (boolean) via the third parameter,
+and connectivity settings, as defined in your database config file will be used:
+
+ <code>$this->load->model('<var>Model_name</var>', '', <kbd>TRUE</kbd>);</code>
+ </li>
+
+
+<li>You can manually pass database connectivity settings via the third parameter:
+
+
+ <code>$config['hostname'] = "localhost";<br />
+ $config['username'] = "myusername";<br />
+ $config['password'] = "mypassword";<br />
+ $config['database'] = "mydatabase";<br />
+ $config['dbdriver'] = "mysql";<br />
+ $config['dbprefix'] = "";<br />
+ $config['pconnect'] = FALSE;<br />
+ $config['db_debug'] = TRUE;<br />
+ $config['active_r'] = TRUE;<br />
+ <br />
+ $this->load->model('<var>Model_name</var>', '', <kbd>$config</kbd>);</code></li>
+</ul>
+
+
+
+
+
+
+
+</div>
+<!-- END CONTENT -->
+
+
+<div id="footer">
+<p>
+Previous Topic:&nbsp;&nbsp;<a href="views.html">Views</a>
+&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
+<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
+<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
+Next Topic:&nbsp;&nbsp;<a href="helpers.html">Helpers</a>
+<p>
+<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>
+</div>
+
+</body>
+</html> \ No newline at end of file