blob: a028a9569d0a334840d0d74a7600b4f2b7fef6e1 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001######
2Models
3######
4
5Models are **optionally** available for those who want to use a more
6traditional MVC approach.
7
Joseph Wensley5b3ea1a2011-10-06 20:54:32 -04008.. contents:: Page Contents
Derek Jones8ede1a22011-10-05 13:34:52 -05009
10What is a Model?
11================
12
13Models are PHP classes that are designed to work with information in
14your database. For example, let's say you use CodeIgniter to manage a
15blog. You might have a model class that contains functions to insert,
16update, and retrieve your blog data. Here is an example of what such a
17model class might look like::
18
Alex Bilbie697b75e2012-06-02 11:22:58 +010019 class Blog_model extends CI_Model {
Derek Jones8ede1a22011-10-05 13:34:52 -050020
Andrey Andreev16a704c2012-11-09 17:25:00 +020021 public $title;
22 public $content;
23 public $date;
Derek Jonesa1360ef2011-10-05 17:22:53 -050024
Andrey Andreev16a704c2012-11-09 17:25:00 +020025 public function __construct()
26 {
27 // Call the CI_Model constructor
28 parent::__construct();
29 }
Derek Jonesa1360ef2011-10-05 17:22:53 -050030
Andrey Andreev16a704c2012-11-09 17:25:00 +020031 public function get_last_ten_entries()
32 {
33 $query = $this->db->get('entries', 10);
34 return $query->result();
35 }
Derek Jonesa1360ef2011-10-05 17:22:53 -050036
Andrey Andreev16a704c2012-11-09 17:25:00 +020037 public function insert_entry()
38 {
39 $this->title = $_POST['title']; // please read the below note
40 $this->content = $_POST['content'];
41 $this->date = time();
Derek Jonesa1360ef2011-10-05 17:22:53 -050042
Andrey Andreev16a704c2012-11-09 17:25:00 +020043 $this->db->insert('entries', $this);
44 }
Derek Jonesa1360ef2011-10-05 17:22:53 -050045
Andrey Andreev16a704c2012-11-09 17:25:00 +020046 public function update_entry()
47 {
48 $this->title = $_POST['title'];
49 $this->content = $_POST['content'];
50 $this->date = time();
Derek Jonesa1360ef2011-10-05 17:22:53 -050051
Andrey Andreev16a704c2012-11-09 17:25:00 +020052 $this->db->update('entries', $this, array('id' => $_POST['id']));
53 }
Derek Jonesa1360ef2011-10-05 17:22:53 -050054
55 }
56
Andrey Andreev16a704c2012-11-09 17:25:00 +020057.. note:: The methods in the above example use the :doc:`Query Builder
58 <../database/query_builder>` database methods.
Derek Jones8ede1a22011-10-05 13:34:52 -050059
Andrey Andreev16a704c2012-11-09 17:25:00 +020060.. note:: For the sake of simplicity in this example we're using ``$_POST``
Derek Jones8ede1a22011-10-05 13:34:52 -050061 directly. This is generally bad practice, and a more common approach
Andrey Andreev16a704c2012-11-09 17:25:00 +020062 would be to use the :doc:`Input Library <../libraries/input>`
63 ``$this->input->post('title')``.
Derek Jones8ede1a22011-10-05 13:34:52 -050064
65Anatomy of a Model
66==================
67
Andrey Andreev16a704c2012-11-09 17:25:00 +020068Model classes are stored in your **application/models/** directory.
69They can be nested within sub-directories if you want this type of
70organization.
Derek Jones8ede1a22011-10-05 13:34:52 -050071
72The basic prototype for a model class is this::
73
Derek Jonesa1360ef2011-10-05 17:22:53 -050074 class Model_name extends CI_Model {
75
Andrey Andreevb94b91a2012-07-04 12:32:14 +030076 public function __construct()
77 {
78 parent::__construct();
79 }
80
Derek Jonesa1360ef2011-10-05 17:22:53 -050081 }
Derek Jones8ede1a22011-10-05 13:34:52 -050082
purwandi89f6f1a2011-10-07 19:58:22 +070083Where **Model_name** is the name of your class. Class names **must** have
Derek Jones8ede1a22011-10-05 13:34:52 -050084the first letter capitalized with the rest of the name lowercase. Make
85sure your class extends the base Model class.
86
87The file name will be a lower case version of your class name. For
88example, if your class is this::
89
Derek Jonesa1360ef2011-10-05 17:22:53 -050090 class User_model extends CI_Model {
91
Andrey Andreevb94b91a2012-07-04 12:32:14 +030092 public function __construct()
93 {
94 parent::__construct();
95 }
96
Derek Jonesa1360ef2011-10-05 17:22:53 -050097 }
Derek Jones8ede1a22011-10-05 13:34:52 -050098
99Your file will be this::
100
101 application/models/user_model.php
102
103Loading a Model
104===============
105
106Your models will typically be loaded and called from within your
Andrey Andreev16a704c2012-11-09 17:25:00 +0200107:doc:`controller <controllers>` methods. To load a model you will use
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300108the following method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500109
Alex Bilbie149c0772012-06-02 11:23:41 +0100110 $this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500111
Andrey Andreev16a704c2012-11-09 17:25:00 +0200112If your model is located in a sub-directory, include the relative path
113from your models directory. For example, if you have a model located at
114*application/models/blog/queries.php* you'll load it using::
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
116 $this->load->model('blog/queries');
117
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300118Once loaded, you will access your model methods using an object with the
119same name as your class::
Derek Jones8ede1a22011-10-05 13:34:52 -0500120
Alex Bilbie149c0772012-06-02 11:23:41 +0100121 $this->load->model('model_name');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500122
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300123 $this->model_name->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500124
125If you would like your model assigned to a different object name you can
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300126specify it via the second parameter of the loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500127
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300128 $this->load->model('model_name', 'foobar');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500129
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300130 $this->foobar->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500131
132Here is an example of a controller, that loads a model, then serves a
133view::
134
Derek Jonesa1360ef2011-10-05 17:22:53 -0500135 class Blog_controller extends CI_Controller {
136
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300137 public function blog()
138 {
139 $this->load->model('blog');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500140
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300141 $data['query'] = $this->Blog->get_last_ten_entries();
Derek Jonesa1360ef2011-10-05 17:22:53 -0500142
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300143 $this->load->view('blog', $data);
144 }
Derek Jonesa1360ef2011-10-05 17:22:53 -0500145 }
146
Derek Jones8ede1a22011-10-05 13:34:52 -0500147
148Auto-loading Models
149===================
150
151If you find that you need a particular model globally throughout your
152application, you can tell CodeIgniter to auto-load it during system
153initialization. This is done by opening the
purwandi89f6f1a2011-10-07 19:58:22 +0700154**application/config/autoload.php** file and adding the model to the
Derek Jones8ede1a22011-10-05 13:34:52 -0500155autoload array.
156
157Connecting to your Database
158===========================
159
160When a model is loaded it does **NOT** connect automatically to your
161database. 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.
Andrey Andreev16a704c2012-11-09 17:25:00 +0200166- You can tell the model loading method to auto-connect by passing
167 TRUE (boolean) via the third parameter, and connectivity settings,
168 as defined in your database config file will be used::
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Alex Bilbie149c0772012-06-02 11:23:41 +0100170 $this->load->model('model_name', '', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500171
172- You can manually pass database connectivity settings via the third
Derek Jonesa1360ef2011-10-05 17:22:53 -0500173 parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -0500174
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300175 $config['hostname'] = 'localhost';
176 $config['username'] = 'myusername';
177 $config['password'] = 'mypassword';
178 $config['database'] = 'mydatabase';
179 $config['dbdriver'] = 'mysqli';
180 $config['dbprefix'] = '';
Derek Jonesa1360ef2011-10-05 17:22:53 -0500181 $config['pconnect'] = FALSE;
182 $config['db_debug'] = TRUE;
183
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300184 $this->load->model('Model_name', '', $config);