blob: 4e52a96480ea24c23caa2be1db0b69de5c634357 [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
vlakoffff3f7de2012-06-16 14:21:32 +020021 public $title = '';
22 public $content = '';
23 public $date = '';
Derek Jonesa1360ef2011-10-05 17:22:53 -050024
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
Andrey Andreevb94b91a2012-07-04 12:32:14 +030075 public function __construct()
76 {
77 parent::__construct();
78 }
79
Derek Jonesa1360ef2011-10-05 17:22:53 -050080 }
Derek Jones8ede1a22011-10-05 13:34:52 -050081
purwandi89f6f1a2011-10-07 19:58:22 +070082Where **Model_name** is the name of your class. Class names **must** have
Derek Jones8ede1a22011-10-05 13:34:52 -050083the first letter capitalized with the rest of the name lowercase. Make
84sure your class extends the base Model class.
85
86The file name will be a lower case version of your class name. For
87example, if your class is this::
88
Derek Jonesa1360ef2011-10-05 17:22:53 -050089 class User_model extends CI_Model {
90
Andrey Andreevb94b91a2012-07-04 12:32:14 +030091 public function __construct()
92 {
93 parent::__construct();
94 }
95
Derek Jonesa1360ef2011-10-05 17:22:53 -050096 }
Derek Jones8ede1a22011-10-05 13:34:52 -050097
98Your file will be this::
99
100 application/models/user_model.php
101
102Loading a Model
103===============
104
105Your models will typically be loaded and called from within your
106:doc:`controller <controllers>` functions. To load a model you will use
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300107the following method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500108
Alex Bilbie149c0772012-06-02 11:23:41 +0100109 $this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
111If your model is located in a sub-folder, include the relative path from
112your models folder. For example, if you have a model located at
113application/models/blog/queries.php you'll load it using::
114
115 $this->load->model('blog/queries');
116
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300117Once loaded, you will access your model methods using an object with the
118same name as your class::
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
Alex Bilbie149c0772012-06-02 11:23:41 +0100120 $this->load->model('model_name');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500121
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300122 $this->model_name->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500123
124If you would like your model assigned to a different object name you can
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300125specify it via the second parameter of the loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300127 $this->load->model('model_name', 'foobar');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500128
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300129 $this->foobar->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131Here is an example of a controller, that loads a model, then serves a
132view::
133
Derek Jonesa1360ef2011-10-05 17:22:53 -0500134 class Blog_controller extends CI_Controller {
135
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300136 public function blog()
137 {
138 $this->load->model('blog');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500139
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300140 $data['query'] = $this->Blog->get_last_ten_entries();
Derek Jonesa1360ef2011-10-05 17:22:53 -0500141
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300142 $this->load->view('blog', $data);
143 }
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.
166- You can tell the model loading function to auto-connect by passing
167 TRUE (boolean) via the third parameter, and connectivity settings, as
168 defined in your database config file will be used:
169 ::
170
Alex Bilbie149c0772012-06-02 11:23:41 +0100171 $this->load->model('model_name', '', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500172
173- You can manually pass database connectivity settings via the third
Derek Jonesa1360ef2011-10-05 17:22:53 -0500174 parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -0500175
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300176 $config['hostname'] = 'localhost';
177 $config['username'] = 'myusername';
178 $config['password'] = 'mypassword';
179 $config['database'] = 'mydatabase';
180 $config['dbdriver'] = 'mysqli';
181 $config['dbprefix'] = '';
Derek Jonesa1360ef2011-10-05 17:22:53 -0500182 $config['pconnect'] = FALSE;
183 $config['db_debug'] = TRUE;
184
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300185 $this->load->model('Model_name', '', $config);