Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ###### |
| 2 | Models |
| 3 | ###### |
| 4 | |
| 5 | Models are **optionally** available for those who want to use a more |
| 6 | traditional MVC approach. |
| 7 | |
Joseph Wensley | 5b3ea1a | 2011-10-06 20:54:32 -0400 | [diff] [blame] | 8 | .. contents:: Page Contents |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 9 | |
| 10 | What is a Model? |
| 11 | ================ |
| 12 | |
| 13 | Models are PHP classes that are designed to work with information in |
| 14 | your database. For example, let's say you use CodeIgniter to manage a |
| 15 | blog. You might have a model class that contains functions to insert, |
| 16 | update, and retrieve your blog data. Here is an example of what such a |
| 17 | model class might look like:: |
| 18 | |
Alex Bilbie | 697b75e | 2012-06-02 11:22:58 +0100 | [diff] [blame] | 19 | class Blog_model extends CI_Model { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
vlakoff | ff3f7de | 2012-06-16 14:21:32 +0200 | [diff] [blame] | 21 | public $title = ''; |
| 22 | public $content = ''; |
| 23 | public $date = ''; |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 24 | |
| 25 | function __construct() |
| 26 | { |
| 27 | // Call the Model constructor |
| 28 | parent::__construct(); |
| 29 | } |
| 30 | |
| 31 | function get_last_ten_entries() |
| 32 | { |
| 33 | $query = $this->db->get('entries', 10); |
| 34 | return $query->result(); |
| 35 | } |
| 36 | |
| 37 | function insert_entry() |
| 38 | { |
| 39 | $this->title = $_POST['title']; // please read the below note |
| 40 | $this->content = $_POST['content']; |
| 41 | $this->date = time(); |
| 42 | |
| 43 | $this->db->insert('entries', $this); |
| 44 | } |
| 45 | |
| 46 | function update_entry() |
| 47 | { |
| 48 | $this->title = $_POST['title']; |
| 49 | $this->content = $_POST['content']; |
| 50 | $this->date = time(); |
| 51 | |
| 52 | $this->db->update('entries', $this, array('id' => $_POST['id'])); |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | .. note:: The functions in the above example use the :doc:`Active |
Jamie Rumbelow | 7efad20 | 2012-02-19 12:37:00 +0000 | [diff] [blame] | 58 | Record <../database/query_builder>` database functions. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 59 | |
| 60 | .. note:: For the sake of simplicity in this example we're using $_POST |
| 61 | directly. This is generally bad practice, and a more common approach |
| 62 | would be to use the :doc:`Input Class <../libraries/input>` |
| 63 | $this->input->post('title') |
| 64 | |
| 65 | Anatomy of a Model |
| 66 | ================== |
| 67 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 68 | Model classes are stored in your **application/models/ folder**. They can be |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | nested within sub-folders if you want this type of organization. |
| 70 | |
| 71 | The basic prototype for a model class is this:: |
| 72 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 73 | class Model_name extends CI_Model { |
| 74 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 75 | public function __construct() |
| 76 | { |
| 77 | parent::__construct(); |
| 78 | } |
| 79 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 80 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 81 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 82 | Where **Model_name** is the name of your class. Class names **must** have |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 83 | the first letter capitalized with the rest of the name lowercase. Make |
| 84 | sure your class extends the base Model class. |
| 85 | |
| 86 | The file name will be a lower case version of your class name. For |
| 87 | example, if your class is this:: |
| 88 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 89 | class User_model extends CI_Model { |
| 90 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 91 | public function __construct() |
| 92 | { |
| 93 | parent::__construct(); |
| 94 | } |
| 95 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 96 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 97 | |
| 98 | Your file will be this:: |
| 99 | |
| 100 | application/models/user_model.php |
| 101 | |
| 102 | Loading a Model |
| 103 | =============== |
| 104 | |
| 105 | Your models will typically be loaded and called from within your |
| 106 | :doc:`controller <controllers>` functions. To load a model you will use |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 107 | the following method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 108 | |
Alex Bilbie | 149c077 | 2012-06-02 11:23:41 +0100 | [diff] [blame] | 109 | $this->load->model('model_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
| 111 | If your model is located in a sub-folder, include the relative path from |
| 112 | your models folder. For example, if you have a model located at |
| 113 | application/models/blog/queries.php you'll load it using:: |
| 114 | |
| 115 | $this->load->model('blog/queries'); |
| 116 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 117 | Once loaded, you will access your model methods using an object with the |
| 118 | same name as your class:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 119 | |
Alex Bilbie | 149c077 | 2012-06-02 11:23:41 +0100 | [diff] [blame] | 120 | $this->load->model('model_name'); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 121 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 122 | $this->model_name->method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 123 | |
| 124 | If you would like your model assigned to a different object name you can |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 125 | specify it via the second parameter of the loading method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 126 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 127 | $this->load->model('model_name', 'foobar'); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 128 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 129 | $this->foobar->method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
| 131 | Here is an example of a controller, that loads a model, then serves a |
| 132 | view:: |
| 133 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 134 | class Blog_controller extends CI_Controller { |
| 135 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 136 | public function blog() |
| 137 | { |
| 138 | $this->load->model('blog'); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 139 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 140 | $data['query'] = $this->Blog->get_last_ten_entries(); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 141 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 142 | $this->load->view('blog', $data); |
| 143 | } |
| 144 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 145 | } |
| 146 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 147 | |
| 148 | Auto-loading Models |
| 149 | =================== |
| 150 | |
| 151 | If you find that you need a particular model globally throughout your |
| 152 | application, you can tell CodeIgniter to auto-load it during system |
| 153 | initialization. This is done by opening the |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 154 | **application/config/autoload.php** file and adding the model to the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 155 | autoload array. |
| 156 | |
| 157 | Connecting to your Database |
| 158 | =========================== |
| 159 | |
| 160 | When a model is loaded it does **NOT** connect automatically to your |
| 161 | database. The following options for connecting are available to you: |
| 162 | |
| 163 | - You can connect using the standard database methods :doc:`described |
| 164 | here <../database/connecting>`, either from within your |
| 165 | Controller class or your Model class. |
| 166 | - You can tell the model loading function to auto-connect by passing |
| 167 | TRUE (boolean) via the third parameter, and connectivity settings, as |
| 168 | defined in your database config file will be used: |
| 169 | :: |
| 170 | |
Alex Bilbie | 149c077 | 2012-06-02 11:23:41 +0100 | [diff] [blame] | 171 | $this->load->model('model_name', '', TRUE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
| 173 | - You can manually pass database connectivity settings via the third |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 174 | parameter:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 175 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 176 | $config['hostname'] = 'localhost'; |
| 177 | $config['username'] = 'myusername'; |
| 178 | $config['password'] = 'mypassword'; |
| 179 | $config['database'] = 'mydatabase'; |
| 180 | $config['dbdriver'] = 'mysqli'; |
| 181 | $config['dbprefix'] = ''; |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 182 | $config['pconnect'] = FALSE; |
| 183 | $config['db_debug'] = TRUE; |
| 184 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 185 | $this->load->model('Model_name', '', $config); |