From 37f4b9caa02783e06dd7c5318200113409a0deb1 Mon Sep 17 00:00:00 2001
From: Derek Jones 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
+ 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
retrieve your blog data. Here is an example of what such a model class might look like: Note: The functions in the above example use the Active Record database functions. Note: 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 Input Class $this->input->post('title') Note: 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 Input Class $this->input->post('title') Model classes are stored in your application/models/ folder. They can be nested within sub-folders if you
+ Model classes are stored in your application/models/ folder. They can be nested within sub-folders if you
want this type of organization. The basic prototype for a model class is this: Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase.
+ Where Model_name is the name of your class. Class names must have the first letter capitalized with the rest of the name lowercase.
Make sure your class extends the base Model class. The file name will be a lower case version of your class name. For example, if your class is this: The file name will be a lower case version of your class name. For example, if your class is this:What is a Model?
-
@@ -116,10 +116,10 @@ class Blogmodel extends CI_Model {
}Anatomy of a Model
-
}
}
-
class User_model extends CI_Model {
@@ -161,7 +161,7 @@ To load a model you will use the following function:
$this->load->model('Model_name');
-If your model is located in a sub-folder, include the relative path from your models folder. For example, if +
If your model is located in a sub-folder, include the relative path from your models folder. For example, if you have a model located at application/models/blog/queries.php you'll load it using:
$this->load->model('blog/queries');
@@ -200,12 +200,12 @@ class Blog_controller extends CI_Controller {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.
+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.
When a model is loaded it does NOT connect automatically to your database. The following options for connecting are available to you:
+When a model is loaded it does NOT connect automatically to your database. The following options for connecting are available to you:
CodeIgniter User Guide Version 2.0.3 |
+CodeIgniter User Guide Version 2.1.0 |
Table of Contents Page |