blob: 0b20164e9094a55340a0836241733174e8099039 [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
Derek Jonesa1360ef2011-10-05 17:22:53 -050070 }
Derek Jones8ede1a22011-10-05 13:34:52 -050071
purwandi89f6f1a2011-10-07 19:58:22 +070072Where **Model_name** is the name of your class. Class names **must** have
Derek Jones8ede1a22011-10-05 13:34:52 -050073the first letter capitalized with the rest of the name lowercase. Make
74sure your class extends the base Model class.
75
Andrey Andreev20292312013-07-22 14:29:10 +030076The file name must match the class name. For example, if this is your class::
Derek Jones8ede1a22011-10-05 13:34:52 -050077
Derek Jonesa1360ef2011-10-05 17:22:53 -050078 class User_model extends CI_Model {
79
Derek Jonesa1360ef2011-10-05 17:22:53 -050080 }
Derek Jones8ede1a22011-10-05 13:34:52 -050081
82Your file will be this::
83
Andrey Andreev20292312013-07-22 14:29:10 +030084 application/models/User_model.php
Derek Jones8ede1a22011-10-05 13:34:52 -050085
86Loading a Model
87===============
88
89Your models will typically be loaded and called from within your
Andrey Andreev16a704c2012-11-09 17:25:00 +020090:doc:`controller <controllers>` methods. To load a model you will use
Andrey Andreevb94b91a2012-07-04 12:32:14 +030091the following method::
Derek Jones8ede1a22011-10-05 13:34:52 -050092
Alex Bilbie149c0772012-06-02 11:23:41 +010093 $this->load->model('model_name');
Derek Jones8ede1a22011-10-05 13:34:52 -050094
Andrey Andreev16a704c2012-11-09 17:25:00 +020095If your model is located in a sub-directory, include the relative path
96from your models directory. For example, if you have a model located at
Andrey Andreev20292312013-07-22 14:29:10 +030097*application/models/blog/Queries.php* you'll load it using::
Derek Jones8ede1a22011-10-05 13:34:52 -050098
99 $this->load->model('blog/queries');
100
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300101Once loaded, you will access your model methods using an object with the
102same name as your class::
Derek Jones8ede1a22011-10-05 13:34:52 -0500103
Alex Bilbie149c0772012-06-02 11:23:41 +0100104 $this->load->model('model_name');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500105
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300106 $this->model_name->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500107
108If you would like your model assigned to a different object name you can
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300109specify it via the second parameter of the loading method::
Derek Jones8ede1a22011-10-05 13:34:52 -0500110
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300111 $this->load->model('model_name', 'foobar');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500112
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300113 $this->foobar->method();
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
115Here is an example of a controller, that loads a model, then serves a
116view::
117
Derek Jonesa1360ef2011-10-05 17:22:53 -0500118 class Blog_controller extends CI_Controller {
119
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300120 public function blog()
121 {
122 $this->load->model('blog');
Derek Jonesa1360ef2011-10-05 17:22:53 -0500123
Andrey Andreev38269bb2014-10-22 12:46:36 +0300124 $data['query'] = $this->blog->get_last_ten_entries();
Derek Jonesa1360ef2011-10-05 17:22:53 -0500125
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300126 $this->load->view('blog', $data);
127 }
Derek Jonesa1360ef2011-10-05 17:22:53 -0500128 }
129
Derek Jones8ede1a22011-10-05 13:34:52 -0500130
131Auto-loading Models
132===================
133
134If you find that you need a particular model globally throughout your
135application, you can tell CodeIgniter to auto-load it during system
136initialization. This is done by opening the
purwandi89f6f1a2011-10-07 19:58:22 +0700137**application/config/autoload.php** file and adding the model to the
Derek Jones8ede1a22011-10-05 13:34:52 -0500138autoload array.
139
140Connecting to your Database
141===========================
142
143When a model is loaded it does **NOT** connect automatically to your
144database. The following options for connecting are available to you:
145
146- You can connect using the standard database methods :doc:`described
147 here <../database/connecting>`, either from within your
148 Controller class or your Model class.
Andrey Andreev16a704c2012-11-09 17:25:00 +0200149- You can tell the model loading method to auto-connect by passing
150 TRUE (boolean) via the third parameter, and connectivity settings,
151 as defined in your database config file will be used::
Derek Jones8ede1a22011-10-05 13:34:52 -0500152
Alex Bilbie149c0772012-06-02 11:23:41 +0100153 $this->load->model('model_name', '', TRUE);
Derek Jones8ede1a22011-10-05 13:34:52 -0500154
155- You can manually pass database connectivity settings via the third
Derek Jonesa1360ef2011-10-05 17:22:53 -0500156 parameter::
Derek Jones8ede1a22011-10-05 13:34:52 -0500157
Andrey Andreevb94b91a2012-07-04 12:32:14 +0300158 $config['hostname'] = 'localhost';
159 $config['username'] = 'myusername';
160 $config['password'] = 'mypassword';
161 $config['database'] = 'mydatabase';
162 $config['dbdriver'] = 'mysqli';
163 $config['dbprefix'] = '';
Derek Jonesa1360ef2011-10-05 17:22:53 -0500164 $config['pconnect'] = FALSE;
165 $config['db_debug'] = TRUE;
166
Andrey Andreev4a567782017-11-20 09:58:03 +0200167 $this->load->model('model_name', '', $config);