blob: eb842e927b746733c2cd6ace56c10ff5a7f360a2 [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 get_last_ten_entries()
26 {
27 $query = $this->db->get('entries', 10);
28 return $query->result();
29 }
Derek Jonesa1360ef2011-10-05 17:22:53 -050030
Andrey Andreev16a704c2012-11-09 17:25:00 +020031 public function insert_entry()
32 {
33 $this->title = $_POST['title']; // please read the below note
34 $this->content = $_POST['content'];
35 $this->date = time();
Derek Jonesa1360ef2011-10-05 17:22:53 -050036
Andrey Andreev16a704c2012-11-09 17:25:00 +020037 $this->db->insert('entries', $this);
38 }
Derek Jonesa1360ef2011-10-05 17:22:53 -050039
Andrey Andreev16a704c2012-11-09 17:25:00 +020040 public function update_entry()
41 {
42 $this->title = $_POST['title'];
43 $this->content = $_POST['content'];
44 $this->date = time();
Derek Jonesa1360ef2011-10-05 17:22:53 -050045
Andrey Andreev16a704c2012-11-09 17:25:00 +020046 $this->db->update('entries', $this, array('id' => $_POST['id']));
47 }
Derek Jonesa1360ef2011-10-05 17:22:53 -050048
49 }
50
Andrey Andreev16a704c2012-11-09 17:25:00 +020051.. note:: The methods in the above example use the :doc:`Query Builder
52 <../database/query_builder>` database methods.
Derek Jones8ede1a22011-10-05 13:34:52 -050053
Andrey Andreev16a704c2012-11-09 17:25:00 +020054.. note:: For the sake of simplicity in this example we're using ``$_POST``
Derek Jones8ede1a22011-10-05 13:34:52 -050055 directly. This is generally bad practice, and a more common approach
Andrey Andreev16a704c2012-11-09 17:25:00 +020056 would be to use the :doc:`Input Library <../libraries/input>`
57 ``$this->input->post('title')``.
Derek Jones8ede1a22011-10-05 13:34:52 -050058
59Anatomy of a Model
60==================
61
Andrey Andreev16a704c2012-11-09 17:25:00 +020062Model classes are stored in your **application/models/** directory.
63They can be nested within sub-directories if you want this type of
64organization.
Derek Jones8ede1a22011-10-05 13:34:52 -050065
66The basic prototype for a model class is this::
67
Derek Jonesa1360ef2011-10-05 17:22:53 -050068 class Model_name extends CI_Model {
69
Andrey Andreevb94b91a2012-07-04 12:32:14 +030070 public function __construct()
71 {
72 parent::__construct();
Andrey Andreev0c48f392016-12-16 14:52:54 +020073 // Your own constructor code
Andrey Andreevb94b91a2012-07-04 12:32:14 +030074 }
75
Derek Jonesa1360ef2011-10-05 17:22:53 -050076 }
Derek Jones8ede1a22011-10-05 13:34:52 -050077
purwandi89f6f1a2011-10-07 19:58:22 +070078Where **Model_name** is the name of your class. Class names **must** have
Derek Jones8ede1a22011-10-05 13:34:52 -050079the first letter capitalized with the rest of the name lowercase. Make
80sure your class extends the base Model class.
81
Andrey Andreev20292312013-07-22 14:29:10 +030082The file name must match the class name. For example, if this is your class::
Derek Jones8ede1a22011-10-05 13:34:52 -050083
Derek Jonesa1360ef2011-10-05 17:22:53 -050084 class User_model extends CI_Model {
85
Andrey Andreevb94b91a2012-07-04 12:32:14 +030086 public function __construct()
87 {
88 parent::__construct();
Andrey Andreev0c48f392016-12-16 14:52:54 +020089 // Your own constructor code
Andrey Andreevb94b91a2012-07-04 12:32:14 +030090 }
91
Derek Jonesa1360ef2011-10-05 17:22:53 -050092 }
Derek Jones8ede1a22011-10-05 13:34:52 -050093
94Your file will be this::
95
Andrey Andreev20292312013-07-22 14:29:10 +030096 application/models/User_model.php
Derek Jones8ede1a22011-10-05 13:34:52 -050097
98Loading a Model
99===============
100
101Your models will typically be loaded and called from within your
Andrey Andreev16a704c2012-11-09 17:25:00 +0200102:doc:`controller <controllers>` methods. To load a model you will use
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300103the following method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Alex Bilbie149c0772012-06-02 11:23:41 +0100105 $this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Andrey Andreev16a704c2012-11-09 17:25:00 +0200107If your model is located in a sub-directory, include the relative path
108from your models directory. For example, if you have a model located at
Andrey Andreev20292312013-07-22 14:29:10 +0300109*application/models/blog/Queries.php* you'll load it using::
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
111 $this->load->model('blog/queries');
112
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300113Once loaded, you will access your model methods using an object with the
114same name as your class::
Derek Jones8ede1a22011-10-05 13:34:52 -0500115
Alex Bilbie149c0772012-06-02 11:23:41 +0100116 $this->load->model('model_name');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500117
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300118 $this->model_name->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500119
120If you would like your model assigned to a different object name you can
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300121specify it via the second parameter of the loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500122
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300123 $this->load->model('model_name', 'foobar');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500124
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300125 $this->foobar->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500126
127Here is an example of a controller, that loads a model, then serves a
128view::
129
Derek Jonesa1360ef2011-10-05 17:22:53 -0500130 class Blog_controller extends CI_Controller {
131
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300132 public function blog()
133 {
134 $this->load->model('blog');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500135
Andrey Andreev38269bb2014-10-22 12:46:36 +0300136 $data['query'] = $this->blog->get_last_ten_entries();
Derek Jonesa1360ef2011-10-05 17:22:53 -0500137
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300138 $this->load->view('blog', $data);
139 }
Derek Jonesa1360ef2011-10-05 17:22:53 -0500140 }
141
Derek Jones8ede1a22011-10-05 13:34:52 -0500142
143Auto-loading Models
144===================
145
146If you find that you need a particular model globally throughout your
147application, you can tell CodeIgniter to auto-load it during system
148initialization. This is done by opening the
purwandi89f6f1a2011-10-07 19:58:22 +0700149**application/config/autoload.php** file and adding the model to the
Derek Jones8ede1a22011-10-05 13:34:52 -0500150autoload array.
151
152Connecting to your Database
153===========================
154
155When a model is loaded it does **NOT** connect automatically to your
156database. The following options for connecting are available to you:
157
158- You can connect using the standard database methods :doc:`described
159 here <../database/connecting>`, either from within your
160 Controller class or your Model class.
Andrey Andreev16a704c2012-11-09 17:25:00 +0200161- You can tell the model loading method to auto-connect by passing
162 TRUE (boolean) via the third parameter, and connectivity settings,
163 as defined in your database config file will be used::
Derek Jones8ede1a22011-10-05 13:34:52 -0500164
Alex Bilbie149c0772012-06-02 11:23:41 +0100165 $this->load->model('model_name', '', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500166
167- You can manually pass database connectivity settings via the third
Derek Jonesa1360ef2011-10-05 17:22:53 -0500168 parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -0500169
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300170 $config['hostname'] = 'localhost';
171 $config['username'] = 'myusername';
172 $config['password'] = 'mypassword';
173 $config['database'] = 'mydatabase';
174 $config['dbdriver'] = 'mysqli';
175 $config['dbprefix'] = '';
Derek Jonesa1360ef2011-10-05 17:22:53 -0500176 $config['pconnect'] = FALSE;
177 $config['db_debug'] = TRUE;
178
Andrey Andreev20292312013-07-22 14:29:10 +0300179 $this->load->model('model_name', '', $config);