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