summaryrefslogtreecommitdiffstats
path: root/user_guide_src/source/general/models.rst
blob: 72acf77b4207cd733c166f1983cd1262ccb4de6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
######
Models
######

Models are **optionally** available for those who want to use a more
traditional MVC approach.

.. contents:: Page Contents

What is a Model?
================

Models are PHP classes that are designed to work with information in
your database. For example, let's say you use CodeIgniter to manage a
blog. You might have a model class that contains functions to insert,
update, and retrieve your blog data. Here is an example of what such a
model class might look like::

	class Blog_model extends CI_Model {

	    var $title   = '';
	    var $content = '';
	    var $date    = '';

	    function __construct()
	    {
	        // Call the Model constructor
	        parent::__construct();
	    }

	    function get_last_ten_entries()
	    {
	        $query = $this->db->get('entries', 10);
	        return $query->result();
	    }

	    function insert_entry()
	    {
	        $this->title   = $_POST['title']; // please read the below note
	        $this->content = $_POST['content'];
	        $this->date    = time();

	        $this->db->insert('entries', $this);
	    }

	    function update_entry()
	    {
	        $this->title   = $_POST['title'];
	        $this->content = $_POST['content'];
	        $this->date    = time();

	        $this->db->update('entries', $this, array('id' => $_POST['id']));
	    }

	}

.. note:: The functions in the above example use the :doc:`Active
	Record <../database/query_builder>` database functions.

.. note:: For the sake of simplicity in this example we're using $_POST
	directly. This is generally bad practice, and a more common approach
	would be to use the :doc:`Input Class <../libraries/input>`
	$this->input->post('title')

Anatomy of a Model
==================

Model classes are stored in your **application/models/ folder**. They can be
nested within sub-folders if you want this type of organization.

The basic prototype for a model class is this::

	class Model_name extends CI_Model {

	    function __construct()
	    {
	        parent::__construct();
	    }
	}

Where **Model_name** is the name of your class. Class names **must** have
the first letter capitalized with the rest of the name lowercase. Make
sure your class extends the base Model class.

The file name will be a lower case version of your class name. For
example, if your class is this::

	class User_model extends CI_Model {

	    function __construct()
	    {
	        parent::__construct();
	    }
	}

Your file will be this::

	application/models/user_model.php

Loading a Model
===============

Your models will typically be loaded and called from within your
:doc:`controller <controllers>` functions. To load a model you will use
the following function::

	$this->load->model('Model_name');

If your model is located in a sub-folder, include the relative path from
your models folder. For example, if you have a model located at
application/models/blog/queries.php you'll load it using::

	$this->load->model('blog/queries');

Once loaded, you will access your model functions using an object with
the same name as your class::

	$this->load->model('Model_name');

	$this->Model_name->function();

If you would like your model assigned to a different object name you can
specify it via the second parameter of the loading function::

	$this->load->model('Model_name', 'fubar');

	$this->fubar->function();

Here is an example of a controller, that loads a model, then serves a
view::

	class Blog_controller extends CI_Controller {

	    function blog()
	    {
	        $this->load->model('Blog');

	        $data['query'] = $this->Blog->get_last_ten_entries();

	        $this->load->view('blog', $data);
	    }
	}
	

Auto-loading Models
===================

If you find that you need a particular model globally throughout your
application, you can tell CodeIgniter to auto-load it during system
initialization. This is done by opening the
**application/config/autoload.php** file and adding the model to the
autoload array.

Connecting to your Database
===========================

When a model is loaded it does **NOT** connect automatically to your
database. The following options for connecting are available to you:

-  You can connect using the standard database methods :doc:`described
   here <../database/connecting>`, either from within your
   Controller class or your Model class.
-  You can tell the model loading function to auto-connect by passing
   TRUE (boolean) via the third parameter, and connectivity settings, as
   defined in your database config file will be used:
   ::

	$this->load->model('Model_name', '', TRUE);

-  You can manually pass database connectivity settings via the third
   parameter::

	$config['hostname'] = "localhost";
	$config['username'] = "myusername";
	$config['password'] = "mypassword";
	$config['database'] = "mydatabase";
	$config['dbdriver'] = "mysql";
	$config['dbprefix'] = "";
	$config['pconnect'] = FALSE;
	$config['db_debug'] = TRUE;

	$this->load->model('Model_name', '', $config);