blob: 0156b0460b3bcb6b53df4c24bf30b3a45902c82d [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
Derek Jonesa1360ef2011-10-05 17:22:53 -050019 class Blogmodel extends CI_Model {
Derek Jones8ede1a22011-10-05 13:34:52 -050020
Derek Jonesa1360ef2011-10-05 17:22:53 -050021 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 Rumbelow7efad202012-02-19 12:37:00 +000058 Record <../database/query_builder>` database functions.
Derek Jones8ede1a22011-10-05 13:34:52 -050059
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
65Anatomy of a Model
66==================
67
purwandi89f6f1a2011-10-07 19:58:22 +070068Model classes are stored in your **application/models/ folder**. They can be
Derek Jones8ede1a22011-10-05 13:34:52 -050069nested within sub-folders if you want this type of organization.
70
71The basic prototype for a model class is this::
72
Derek Jonesa1360ef2011-10-05 17:22:53 -050073 class Model_name extends CI_Model {
74
75 function __construct()
76 {
77 parent::__construct();
78 }
79 }
Derek Jones8ede1a22011-10-05 13:34:52 -050080
purwandi89f6f1a2011-10-07 19:58:22 +070081Where **Model_name** is the name of your class. Class names **must** have
Derek Jones8ede1a22011-10-05 13:34:52 -050082the first letter capitalized with the rest of the name lowercase. Make
83sure your class extends the base Model class.
84
85The file name will be a lower case version of your class name. For
86example, if your class is this::
87
Derek Jonesa1360ef2011-10-05 17:22:53 -050088 class User_model extends CI_Model {
89
90 function __construct()
91 {
92 parent::__construct();
93 }
94 }
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96Your file will be this::
97
98 application/models/user_model.php
99
100Loading a Model
101===============
102
103Your models will typically be loaded and called from within your
104:doc:`controller <controllers>` functions. To load a model you will use
105the following function::
106
107 $this->load->model('Model_name');
108
109If your model is located in a sub-folder, include the relative path from
110your models folder. For example, if you have a model located at
111application/models/blog/queries.php you'll load it using::
112
113 $this->load->model('blog/queries');
114
115Once loaded, you will access your model functions using an object with
116the same name as your class::
117
Derek Jonesa1360ef2011-10-05 17:22:53 -0500118 $this->load->model('Model_name');
119
120 $this->Model_name->function();
Derek Jones8ede1a22011-10-05 13:34:52 -0500121
122If you would like your model assigned to a different object name you can
123specify it via the second parameter of the loading function::
124
Derek Jonesa1360ef2011-10-05 17:22:53 -0500125 $this->load->model('Model_name', 'fubar');
126
127 $this->fubar->function();
Derek Jones8ede1a22011-10-05 13:34:52 -0500128
129Here is an example of a controller, that loads a model, then serves a
130view::
131
Derek Jonesa1360ef2011-10-05 17:22:53 -0500132 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 Jones8ede1a22011-10-05 13:34:52 -0500144
145Auto-loading Models
146===================
147
148If you find that you need a particular model globally throughout your
149application, you can tell CodeIgniter to auto-load it during system
150initialization. This is done by opening the
purwandi89f6f1a2011-10-07 19:58:22 +0700151**application/config/autoload.php** file and adding the model to the
Derek Jones8ede1a22011-10-05 13:34:52 -0500152autoload array.
153
154Connecting to your Database
155===========================
156
157When a model is loaded it does **NOT** connect automatically to your
158database. 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 Jonesa1360ef2011-10-05 17:22:53 -0500171 parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
Derek Jonesa1360ef2011-10-05 17:22:53 -0500173 $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 Jones8ede1a22011-10-05 13:34:52 -0500183
184