From 8ede1a2ecbb62577afd32996956c5feaf7ddf9b6 Mon Sep 17 00:00:00 2001 From: Derek Jones Date: Wed, 5 Oct 2011 13:34:52 -0500 Subject: replacing the old HTML user guide with a Sphinx-managed user guide --- user_guide/general/models.html | 251 ----------------------------------------- 1 file changed, 251 deletions(-) delete mode 100644 user_guide/general/models.html (limited to 'user_guide/general/models.html') diff --git a/user_guide/general/models.html b/user_guide/general/models.html deleted file mode 100644 index 1696f424a..000000000 --- a/user_guide/general/models.html +++ /dev/null @@ -1,251 +0,0 @@ - - - - - -Models : CodeIgniter User Guide - - - - - - - - - - - - - - - - - - - - -
- - - - - -

CodeIgniter User Guide Version 2.0.3

-
- - - - - - - - - -
- - -
- - - -
- -

Models

- -

Models are optionally available for those who want to use a more traditional MVC approach.

- - - - - - - -

What is a Model?

- -

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:

- - -class Blogmodel extends CI_Model {
-
-    var $title   = '';
-    var $content = '';
-    var $date    = '';
-
-    function __construct()
-    {
-        // Call the Model constructor
-        parent::__construct();
-    }
-    
-    function get_last_ten_entries()
-    {
-        $query = $this->db->get('entries', 10);
-        return $query->result();
-    }
-
-    function insert_entry()
-    {
-        $this->title   = $_POST['title']; // please read the below note
-        $this->content = $_POST['content'];
-        $this->date    = time();
-
-        $this->db->insert('entries', $this);
-    }
-
-    function update_entry()
-    {
-        $this->title   = $_POST['title'];
-        $this->content = $_POST['content'];
-        $this->date    = time();
-
-        $this->db->update('entries', $this, array('id' => $_POST['id']));
-    }
-
-}
- -

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')

-

Anatomy of a Model

- -

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:

- - - -class Model_name extends CI_Model {
-
-    function __construct()
-    {
-        parent::__construct();
-    }
-}
- -

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:

- - -class User_model extends CI_Model {
-
-    function __construct()
-    {
-        parent::__construct();
-    }
-}
- -

Your file will be this:

- -application/models/user_model.php - - - -

Loading a Model

- -

Your models will typically be loaded and called from within your controller functions. -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 -you have a model located at application/models/blog/queries.php you'll load it using:

- -$this->load->model('blog/queries'); - - -

Once loaded, you will access your model functions using an object with the same name as your class:

- - -$this->load->model('Model_name');
-
-$this->Model_name->function(); -
- -

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading -function:

- - - -$this->load->model('Model_name', 'fubar');
-
-$this->fubar->function(); -
- -

Here is an example of a controller, that loads a model, then serves a view:

- - -class Blog_controller extends CI_Controller {
-
-    function blog()
-    {
-        $this->load->model('Blog');
-
-        $data['query'] = $this->Blog->get_last_ten_entries();

-        $this->load->view('blog', $data);
-    }
-}
- -

Auto-loading Models

-

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.

- - -

Connecting to your Database

- -

When a model is loaded it does NOT connect automatically to your database. The following options for connecting are available to you:

- - - - -
- - - - - - - \ No newline at end of file -- cgit v1.2.3-24-g4f1b