summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/general/models.rst
diff options
context:
space:
mode:
authorAndrey Andreev <narf@devilix.net>2018-01-13 12:57:11 +0100
committerAndrey Andreev <narf@devilix.net>2018-01-13 12:57:11 +0100
commit02a0518e1c56ba3506f370362ac5ebdb8a138312 (patch)
tree8b4684192b403dcae17d298eb4fc9e7ab001acad /user_guide_src/source/general/models.rst
parentf0b9735d5857e6c07c8f5824b13cff4828db3cfe (diff)
[ci skip] 3.1.7 release
Diffstat (limited to 'user_guide_src/source/general/models.rst')
-rw-r--r--user_guide_src/source/general/models.rst167
1 files changed, 0 insertions, 167 deletions
diff --git a/user_guide_src/source/general/models.rst b/user_guide_src/source/general/models.rst
deleted file mode 100644
index 0b20164e9..000000000
--- a/user_guide_src/source/general/models.rst
+++ /dev/null
@@ -1,167 +0,0 @@
-######
-Models
-######
-
-Models are **optionally** available for those who want to use a more
-traditional MVC approach.
-
-.. contents:: Page Contents
-
-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 Blog_model extends CI_Model {
-
- public $title;
- public $content;
- public $date;
-
- 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 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``
- directly. This is generally bad practice, and a more common approach
- 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/** directory.
-They can be nested within sub-directories if you want this type of
-organization.
-
-The basic prototype for a model class is this::
-
- class Model_name extends CI_Model {
-
- }
-
-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 must match the class name. For example, if this is your class::
-
- class User_model extends CI_Model {
-
- }
-
-Your file will be this::
-
- application/models/User_model.php
-
-Loading a Model
-===============
-
-Your models will typically be loaded and called from within your
-: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-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');
-
-Once loaded, you will access your model methods using an object with the
-same name as your class::
-
- $this->load->model('model_name');
-
- $this->model_name->method();
-
-If you would like your model assigned to a different object name you can
-specify it via the second parameter of the loading method::
-
- $this->load->model('model_name', 'foobar');
-
- $this->foobar->method();
-
-Here is an example of a controller, that loads a model, then serves a
-view::
-
- class Blog_controller extends CI_Controller {
-
- public 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:
-
-- 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 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);
-
-- You can manually pass database connectivity settings via the third
- parameter::
-
- $config['hostname'] = 'localhost';
- $config['username'] = 'myusername';
- $config['password'] = 'mypassword';
- $config['database'] = 'mydatabase';
- $config['dbdriver'] = 'mysqli';
- $config['dbprefix'] = '';
- $config['pconnect'] = FALSE;
- $config['db_debug'] = TRUE;
-
- $this->load->model('model_name', '', $config);