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 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 19 | class Blogmodel extends CI_Model { |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 20 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 21 | var $title = ''; |
| 22 | var $content = ''; |
| 23 | var $date = ''; |
| 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 | |
| 75 | function __construct() |
| 76 | { |
| 77 | parent::__construct(); |
| 78 | } |
| 79 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 81 | 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] | 82 | the first letter capitalized with the rest of the name lowercase. Make |
| 83 | sure your class extends the base Model class. |
| 84 | |
| 85 | The file name will be a lower case version of your class name. For |
| 86 | example, if your class is this:: |
| 87 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 88 | class User_model extends CI_Model { |
| 89 | |
| 90 | function __construct() |
| 91 | { |
| 92 | parent::__construct(); |
| 93 | } |
| 94 | } |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 95 | |
| 96 | Your file will be this:: |
| 97 | |
| 98 | application/models/user_model.php |
| 99 | |
| 100 | Loading a Model |
| 101 | =============== |
| 102 | |
| 103 | Your models will typically be loaded and called from within your |
| 104 | :doc:`controller <controllers>` functions. To load a model you will use |
| 105 | the following function:: |
| 106 | |
| 107 | $this->load->model('Model_name'); |
| 108 | |
| 109 | If your model is located in a sub-folder, include the relative path from |
| 110 | your models folder. For example, if you have a model located at |
| 111 | application/models/blog/queries.php you'll load it using:: |
| 112 | |
| 113 | $this->load->model('blog/queries'); |
| 114 | |
| 115 | Once loaded, you will access your model functions using an object with |
| 116 | the same name as your class:: |
| 117 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 118 | $this->load->model('Model_name'); |
| 119 | |
| 120 | $this->Model_name->function(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 121 | |
| 122 | If you would like your model assigned to a different object name you can |
| 123 | specify it via the second parameter of the loading function:: |
| 124 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 125 | $this->load->model('Model_name', 'fubar'); |
| 126 | |
| 127 | $this->fubar->function(); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 128 | |
| 129 | Here is an example of a controller, that loads a model, then serves a |
| 130 | view:: |
| 131 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 132 | class Blog_controller extends CI_Controller { |
| 133 | |
| 134 | function blog() |
| 135 | { |
| 136 | $this->load->model('Blog'); |
| 137 | |
| 138 | $data['query'] = $this->Blog->get_last_ten_entries(); |
| 139 | |
| 140 | $this->load->view('blog', $data); |
| 141 | } |
| 142 | } |
| 143 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 144 | |
| 145 | Auto-loading Models |
| 146 | =================== |
| 147 | |
| 148 | If you find that you need a particular model globally throughout your |
| 149 | application, you can tell CodeIgniter to auto-load it during system |
| 150 | initialization. This is done by opening the |
purwandi | 89f6f1a | 2011-10-07 19:58:22 +0700 | [diff] [blame] | 151 | **application/config/autoload.php** file and adding the model to the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 152 | autoload array. |
| 153 | |
| 154 | Connecting to your Database |
| 155 | =========================== |
| 156 | |
| 157 | When a model is loaded it does **NOT** connect automatically to your |
| 158 | database. The following options for connecting are available to you: |
| 159 | |
| 160 | - You can connect using the standard database methods :doc:`described |
| 161 | here <../database/connecting>`, either from within your |
| 162 | Controller class or your Model class. |
| 163 | - You can tell the model loading function to auto-connect by passing |
| 164 | TRUE (boolean) via the third parameter, and connectivity settings, as |
| 165 | defined in your database config file will be used: |
| 166 | :: |
| 167 | |
| 168 | $this->load->model('Model_name', '', TRUE); |
| 169 | |
| 170 | - You can manually pass database connectivity settings via the third |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 171 | parameter:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 172 | |
Derek Jones | a1360ef | 2011-10-05 17:22:53 -0500 | [diff] [blame] | 173 | $config['hostname'] = "localhost"; |
| 174 | $config['username'] = "myusername"; |
| 175 | $config['password'] = "mypassword"; |
| 176 | $config['database'] = "mydatabase"; |
| 177 | $config['dbdriver'] = "mysql"; |
| 178 | $config['dbprefix'] = ""; |
| 179 | $config['pconnect'] = FALSE; |
| 180 | $config['db_debug'] = TRUE; |
| 181 | |
| 182 | $this->load->model('Model_name', '', $config); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 183 | |
| 184 | |