diff options
author | Derek Allard <derek.allard@ellislab.com> | 2007-10-09 15:25:27 +0200 |
---|---|---|
committer | Derek Allard <derek.allard@ellislab.com> | 2007-10-09 15:25:27 +0200 |
commit | befdc87b1ce6caf47c6f8b1dfa02333dfee8a950 (patch) | |
tree | 58c9c2ecc2c9fdf394a2d1a6d727d3e4ff07a136 | |
parent | 6838f00a708f53f834fb8a98af560177db1d1454 (diff) |
Added the ability to auto-load Models
-rw-r--r-- | system/application/config/autoload.php | 15 | ||||
-rw-r--r-- | system/libraries/Loader.php | 18 | ||||
-rw-r--r-- | user_guide/changelog.html | 1 | ||||
-rw-r--r-- | user_guide/general/autoloader.html | 1 | ||||
-rw-r--r-- | user_guide/general/models.html | 19 |
5 files changed, 45 insertions, 9 deletions
diff --git a/system/application/config/autoload.php b/system/application/config/autoload.php index 8bb4181b5..2dfbe422c 100644 --- a/system/application/config/autoload.php +++ b/system/application/config/autoload.php @@ -23,6 +23,7 @@ | 3. Plugins
| 4. Custom config files
| 5. Language files
+| 6. Models
|
*/
@@ -96,6 +97,20 @@ $autoload['config'] = array(); $autoload['language'] = array();
+
+/*
+| -------------------------------------------------------------------
+| Auto-load Models
+| -------------------------------------------------------------------
+| Prototype:
+|
+| $autoload['model'] = array('model1', 'model2');
+|
+*/
+
+$autoload['model'] = array();
+
+
/*
| -------------------------------------------------------------------
| Auto-load Core Libraries
diff --git a/system/libraries/Loader.php b/system/libraries/Loader.php index 051d3d899..dd639798d 100644 --- a/system/libraries/Loader.php +++ b/system/libraries/Loader.php @@ -107,8 +107,19 @@ class CI_Loader { */
function model($model, $name = '', $db_conn = FALSE)
{
+ if (is_array($model))
+ {
+ foreach($model as $babe)
+ {
+ $this->model($babe);
+ }
+ return;
+ }
+
if ($model == '')
+ {
return;
+ }
// Is the model in a sub-folder? If so, parse out the filename and path.
if (strpos($model, '/') === FALSE)
@@ -855,6 +866,12 @@ class CI_Loader { }
}
+ // Autoload models
+ if (isset($autoload['model']))
+ {
+ $this->model($autoload['model']);
+ }
+
// A little tweak to remain backward compatible
// The $autoload['core'] item was deprecated
if ( ! isset($autoload['libraries']))
@@ -875,6 +892,7 @@ class CI_Loader { // Load the model class.
if (in_array('model', $autoload['libraries']))
{
+ die('made it in!');
$this->model();
$autoload['libraries'] = array_diff($autoload['libraries'], array('model'));
}
diff --git a/user_guide/changelog.html b/user_guide/changelog.html index fdd870a9a..eb2e3ef7a 100644 --- a/user_guide/changelog.html +++ b/user_guide/changelog.html @@ -66,6 +66,7 @@ Change Log <p>Release Date: -- still in development </p>
<ul>
<li>Added Flashdata variables, session_id regeneration and configurable session update times to the <a href="./libraries/sessions.html">Session class.</a></li>
+ <li>Added the ability to <a href="./general/autoloader.html">auto-load</a> <a href="./general/models.html">Models</a></li>
<li>Added $this->DB->save_queries variable to DB driver, enabling queries to get saved or no. Previously they were always saved.</li>
<li>Added <dfn>$assign_to_controller</dfn> variable in the main <kbd>index.php</kbd> file. Anything that this variable contains will be passed automatically to a controller constructor when initialized.</li>
<li>Reorganized the URI and Routes classes for better clarity.</li>
diff --git a/user_guide/general/autoloader.html b/user_guide/general/autoloader.html index 55b3c0d38..c90120cc8 100644 --- a/user_guide/general/autoloader.html +++ b/user_guide/general/autoloader.html @@ -74,6 +74,7 @@ consider auto-loading them for convenience.</p> <li>Plugins found in the "plugins" folder</li>
<li>Custom config files found in the "config" folder</li>
<li>Language files found in the "system/language" folder </li>
+<li>Models found in the "models" folder</li>
</ul>
<p>To autoload resources, open the <var>application/config/autoload.php</var> file and add the item you want
diff --git a/user_guide/general/models.html b/user_guide/general/models.html index f2a676188..b111eacca 100644 --- a/user_guide/general/models.html +++ b/user_guide/general/models.html @@ -70,13 +70,13 @@ Models <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="#auto_load_model">Auto-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>
+
+<h2><a name="what"></a>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 CodeIgniter to manage a blog. You might have a model class that contains functions to insert, update, and
@@ -124,8 +124,8 @@ class Blogmodel extends Model {<br /> <p>Note: The functions in the above example use the <a href="../database/active_record.html">Active Record</a> database functions.</p>
-<a name="anatomy"></a>
-<h2>Anatomy of a Model</h2>
+
+<h2><a name="anatomy"></a>Anatomy of a Model</h2>
<p>Model classes are stored in your <dfn>application/models/</dfn> folder. They can be nested within sub-folders if you
want this type of organization.</p>
@@ -161,8 +161,8 @@ class <var>User_model</var> extends Model {<br /> <code>application/models/<var>user_model.php</var></code>
-<a name="loading"></a>
-<h2>Loading a Model</h2>
+
+<h2><a name="loading"></a>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>
@@ -207,10 +207,11 @@ class Blog_controller extends Controller {<br /> }<br />
}</code>
+<h2><a name="auto_load_model" id="auto_load_model"></a>Auto-loading Models</h2>
+<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 mdoel to the autoload array.</p>
-<a name="conn"></a>
-<h2>Connecting to your Database</h2>
+<h2><a name="conn"></a>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>
|