blob: 55081d12a45498dfe9bc146aaa3d9e500cf3efe4 [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
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
14What is a Model?
15================
16
17Models are PHP classes that are designed to work with information in
18your database. For example, let's say you use CodeIgniter to manage a
19blog. You might have a model class that contains functions to insert,
20update, and retrieve your blog data. Here is an example of what such a
21model class might look like::
22
Derek Jonesa1360ef2011-10-05 17:22:53 -050023 class Blogmodel extends CI_Model {
Derek Jones8ede1a22011-10-05 13:34:52 -050024
Derek Jonesa1360ef2011-10-05 17:22:53 -050025 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 Jones8ede1a22011-10-05 13:34:52 -050063
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
69Anatomy of a Model
70==================
71
72Model classes are stored in your application/models/ folder. They can be
73nested within sub-folders if you want this type of organization.
74
75The basic prototype for a model class is this::
76
Derek Jonesa1360ef2011-10-05 17:22:53 -050077 class Model_name extends CI_Model {
78
79 function __construct()
80 {
81 parent::__construct();
82 }
83 }
Derek Jones8ede1a22011-10-05 13:34:52 -050084
85Where Model_name is the name of your class. Class names **must** have
86the first letter capitalized with the rest of the name lowercase. Make
87sure your class extends the base Model class.
88
89The file name will be a lower case version of your class name. For
90example, if your class is this::
91
Derek Jonesa1360ef2011-10-05 17:22:53 -050092 class User_model extends CI_Model {
93
94 function __construct()
95 {
96 parent::__construct();
97 }
98 }
Derek Jones8ede1a22011-10-05 13:34:52 -050099
100Your file will be this::
101
102 application/models/user_model.php
103
104Loading a Model
105===============
106
107Your models will typically be loaded and called from within your
108:doc:`controller <controllers>` functions. To load a model you will use
109the following function::
110
111 $this->load->model('Model_name');
112
113If your model is located in a sub-folder, include the relative path from
114your models folder. For example, if you have a model located at
115application/models/blog/queries.php you'll load it using::
116
117 $this->load->model('blog/queries');
118
119Once loaded, you will access your model functions using an object with
120the same name as your class::
121
Derek Jonesa1360ef2011-10-05 17:22:53 -0500122 $this->load->model('Model_name');
123
124 $this->Model_name->function();
Derek Jones8ede1a22011-10-05 13:34:52 -0500125
126If you would like your model assigned to a different object name you can
127specify it via the second parameter of the loading function::
128
Derek Jonesa1360ef2011-10-05 17:22:53 -0500129 $this->load->model('Model_name', 'fubar');
130
131 $this->fubar->function();
Derek Jones8ede1a22011-10-05 13:34:52 -0500132
133Here is an example of a controller, that loads a model, then serves a
134view::
135
Derek Jonesa1360ef2011-10-05 17:22:53 -0500136 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 Jones8ede1a22011-10-05 13:34:52 -0500148
149Auto-loading Models
150===================
151
152If you find that you need a particular model globally throughout your
153application, you can tell CodeIgniter to auto-load it during system
154initialization. This is done by opening the
155application/config/autoload.php file and adding the model to the
156autoload array.
157
158Connecting to your Database
159===========================
160
161When a model is loaded it does **NOT** connect automatically to your
162database. 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 Jonesa1360ef2011-10-05 17:22:53 -0500175 parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -0500176
Derek Jonesa1360ef2011-10-05 17:22:53 -0500177 $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 Jones8ede1a22011-10-05 13:34:52 -0500187
188