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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +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 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 25 | public function get_last_ten_entries() |
| 26 | { |
| 27 | $query = $this->db->get('entries', 10); |
| 28 | return $query->result(); |
| 29 | } |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 30 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 31 | public function insert_entry() |
| 32 | { |
| 33 | $this->title = $_POST['title']; // please read the below note |
| 34 | $this->content = $_POST['content']; |
| 35 | $this->date = time(); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 36 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 37 | $this->db->insert('entries', $this); |
| 38 | } |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 39 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 40 | public function update_entry() |
| 41 | { |
| 42 | $this->title = $_POST['title']; |
| 43 | $this->content = $_POST['content']; |
| 44 | $this->date = time(); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 45 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 46 | $this->db->update('entries', $this, array('id' => $_POST['id'])); |
| 47 | } |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 48 | |
| 49 | } |
| 50 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 51 | .. note:: The methods in the above example use the :doc:`Query Builder |
| 52 | <../database/query_builder>` database methods. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 53 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 54 | .. note:: For the sake of simplicity in this example we're using ``$_POST`` |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 55 | directly. This is generally bad practice, and a more common approach |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 56 | would be to use the :doc:`Input Library <../libraries/input>` |
| 57 | ``$this->input->post('title')``. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 58 | |
| 59 | Anatomy of a Model |
| 60 | ================== |
| 61 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 62 | Model classes are stored in your **application/models/** directory. |
| 63 | They can be nested within sub-directories if you want this type of |
| 64 | organization. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
| 66 | The basic prototype for a model class is this:: |
| 67 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 68 | class Model_name extends CI_Model { |
| 69 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 70 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 71 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 72 | 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] | 73 | the first letter capitalized with the rest of the name lowercase. Make |
| 74 | sure your class extends the base Model class. |
| 75 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 76 | The file name must match the class name. For example, if this is your class:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 77 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 78 | class User_model extends CI_Model { |
| 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 | |
| 82 | Your file will be this:: |
| 83 | |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 84 | application/models/User_model.php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 85 | |
| 86 | Loading a Model |
| 87 | =============== |
| 88 | |
| 89 | Your models will typically be loaded and called from within your |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 90 | :doc:`controller <controllers>` methods. To load a model you will use |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 91 | the following method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 92 | |
Alex Bilbie | 149c077 | 2012-06-02 11:23:41 +0100 | [diff] [blame] | 93 | $this->load->model('model_name'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 94 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 95 | If your model is located in a sub-directory, include the relative path |
| 96 | from your models directory. For example, if you have a model located at |
Andrey Andreev | 2029231 | 2013-07-22 14:29:10 +0300 | [diff] [blame] | 97 | *application/models/blog/Queries.php* you'll load it using:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 98 | |
| 99 | $this->load->model('blog/queries'); |
| 100 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 101 | Once loaded, you will access your model methods using an object with the |
| 102 | same name as your class:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 103 | |
Alex Bilbie | 149c077 | 2012-06-02 11:23:41 +0100 | [diff] [blame] | 104 | $this->load->model('model_name'); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 105 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 106 | $this->model_name->method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 107 | |
| 108 | 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] | 109 | specify it via the second parameter of the loading method:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 110 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 111 | $this->load->model('model_name', 'foobar'); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 112 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 113 | $this->foobar->method(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
| 115 | Here is an example of a controller, that loads a model, then serves a |
| 116 | view:: |
| 117 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 118 | class Blog_controller extends CI_Controller { |
| 119 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 120 | public function blog() |
| 121 | { |
| 122 | $this->load->model('blog'); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 123 | |
Andrey Andreev | 38269bb | 2014-10-22 12:46:36 +0300 | [diff] [blame] | 124 | $data['query'] = $this->blog->get_last_ten_entries(); |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 125 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 126 | $this->load->view('blog', $data); |
| 127 | } |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 130 | |
| 131 | Auto-loading Models |
| 132 | =================== |
| 133 | |
| 134 | If you find that you need a particular model globally throughout your |
| 135 | application, you can tell CodeIgniter to auto-load it during system |
| 136 | initialization. This is done by opening the |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 137 | **application/config/autoload.php** file and adding the model to the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 138 | autoload array. |
| 139 | |
| 140 | Connecting to your Database |
| 141 | =========================== |
| 142 | |
| 143 | When a model is loaded it does **NOT** connect automatically to your |
| 144 | database. The following options for connecting are available to you: |
| 145 | |
| 146 | - You can connect using the standard database methods :doc:`described |
| 147 | here <../database/connecting>`, either from within your |
| 148 | Controller class or your Model class. |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 149 | - You can tell the model loading method to auto-connect by passing |
| 150 | TRUE (boolean) via the third parameter, and connectivity settings, |
| 151 | as defined in your database config file will be used:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | |
Alex Bilbie | 149c077 | 2012-06-02 11:23:41 +0100 | [diff] [blame] | 153 | $this->load->model('model_name', '', TRUE); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 154 | |
| 155 | - You can manually pass database connectivity settings via the third |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 156 | parameter:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 157 | |
Andrey Andreev | b94b91a | 2012-07-04 12:32:14 +0300 | [diff] [blame] | 158 | $config['hostname'] = 'localhost'; |
| 159 | $config['username'] = 'myusername'; |
| 160 | $config['password'] = 'mypassword'; |
| 161 | $config['database'] = 'mydatabase'; |
| 162 | $config['dbdriver'] = 'mysqli'; |
| 163 | $config['dbprefix'] = ''; |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 164 | $config['pconnect'] = FALSE; |
| 165 | $config['db_debug'] = TRUE; |
| 166 | |
Andrey Andreev | 4a56778 | 2017-11-20 09:58:03 +0200 | [diff] [blame^] | 167 | $this->load->model('model_name', '', $config); |