diff options
author | Jonathon Hill <jhill@brandmovers.com> | 2012-11-12 14:51:41 +0100 |
---|---|---|
committer | Jonathon Hill <jhill@brandmovers.com> | 2012-11-12 14:51:41 +0100 |
commit | 3978fc33d82dd7f778d1adbf30744f4dfac41c25 (patch) | |
tree | f32be1ae610f0cfeff65c35abecd14e8ea5cadc6 /user_guide_src/source/general/models.rst | |
parent | 275cf274860c6ed181d50b398efd3a21d7ba9135 (diff) | |
parent | a9ab46d7a031bda304eb9b6658ffaf693b8d9bcb (diff) |
Merge remote-tracking branch 'upstream/develop' into develop
Conflicts:
user_guide_src/source/changelog.rst
Signed-off-by: Jonathon Hill <jhill@brandmovers.com>
Diffstat (limited to 'user_guide_src/source/general/models.rst')
-rw-r--r-- | user_guide_src/source/general/models.rst | 97 |
1 files changed, 48 insertions, 49 deletions
diff --git a/user_guide_src/source/general/models.rst b/user_guide_src/source/general/models.rst index 4e52a9648..a028a9569 100644 --- a/user_guide_src/source/general/models.rst +++ b/user_guide_src/source/general/models.rst @@ -18,55 +18,56 @@ model class might look like:: class Blog_model extends CI_Model { - public $title = ''; - public $content = ''; - public $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'])); - } + public $title; + public $content; + public $date; + + public function __construct() + { + // Call the CI_Model constructor + parent::__construct(); + } + + public function get_last_ten_entries() + { + $query = $this->db->get('entries', 10); + return $query->result(); + } + + public function insert_entry() + { + $this->title = $_POST['title']; // please read the below note + $this->content = $_POST['content']; + $this->date = time(); + + $this->db->insert('entries', $this); + } + + public 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 :doc:`Active - Record <../database/query_builder>` database functions. +.. note:: The methods in the above example use the :doc:`Query Builder + <../database/query_builder>` database methods. -.. note:: For the sake of simplicity in this example we're using $_POST +.. 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 :doc:`Input Class <../libraries/input>` - $this->input->post('title') + would be to use the :doc:`Input Library <../libraries/input>` + ``$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. +Model classes are stored in your **application/models/** directory. +They can be nested within sub-directories if you want this type of +organization. The basic prototype for a model class is this:: @@ -103,14 +104,14 @@ Loading a Model =============== Your models will typically be loaded and called from within your -:doc:`controller <controllers>` functions. To load a model you will use +:doc:`controller <controllers>` methods. To load a model you will use the following method:: $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:: +If your model is located in a sub-directory, include the relative path +from your models directory. For example, if you have a model located at +*application/models/blog/queries.php* you'll load it using:: $this->load->model('blog/queries'); @@ -141,7 +142,6 @@ view:: $this->load->view('blog', $data); } - } @@ -163,10 +163,9 @@ database. The following options for connecting are available to you: - You can connect using the standard database methods :doc:`described here <../database/connecting>`, either from within your Controller class or your Model class. -- You can tell the model loading function to auto-connect by passing - TRUE (boolean) via the third parameter, and connectivity settings, as - defined in your database config file will be used: - :: +- You can tell the model loading method to auto-connect by passing + TRUE (boolean) via the third parameter, and connectivity settings, + as defined in your database config file will be used:: $this->load->model('model_name', '', TRUE); |