blob: e26207cc2d1d255307400d5f2b02338bfc355e3b [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
23 class Blogmodel 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']));     } }
24
25Note: The functions in the above example use the :doc:`Active
26Record <../database/active_record>` database functions.
27
28.. note:: For the sake of simplicity in this example we're using $_POST
29 directly. This is generally bad practice, and a more common approach
30 would be to use the :doc:`Input Class <../libraries/input>`
31 $this->input->post('title')
32
33Anatomy of a Model
34==================
35
36Model classes are stored in your application/models/ folder. They can be
37nested within sub-folders if you want this type of organization.
38
39The basic prototype for a model class is this::
40
41 class Model_name extends CI_Model {     function __construct()     {         parent::__construct();     } }
42
43Where Model_name is the name of your class. Class names **must** have
44the first letter capitalized with the rest of the name lowercase. Make
45sure your class extends the base Model class.
46
47The file name will be a lower case version of your class name. For
48example, if your class is this::
49
50 class User_model extends CI_Model {     function __construct()     {         parent::__construct();     } }
51
52Your file will be this::
53
54 application/models/user_model.php
55
56Loading a Model
57===============
58
59Your models will typically be loaded and called from within your
60:doc:`controller <controllers>` functions. To load a model you will use
61the following function::
62
63 $this->load->model('Model_name');
64
65If your model is located in a sub-folder, include the relative path from
66your models folder. For example, if you have a model located at
67application/models/blog/queries.php you'll load it using::
68
69 $this->load->model('blog/queries');
70
71Once loaded, you will access your model functions using an object with
72the same name as your class::
73
74 $this->load->model('Model_name'); $this->Model_name->function();
75
76If you would like your model assigned to a different object name you can
77specify it via the second parameter of the loading function::
78
79 $this->load->model('Model_name', 'fubar'); $this->fubar->function();
80
81Here is an example of a controller, that loads a model, then serves a
82view::
83
84 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);     } }
85
86Auto-loading Models
87===================
88
89If you find that you need a particular model globally throughout your
90application, you can tell CodeIgniter to auto-load it during system
91initialization. This is done by opening the
92application/config/autoload.php file and adding the model to the
93autoload array.
94
95Connecting to your Database
96===========================
97
98When a model is loaded it does **NOT** connect automatically to your
99database. The following options for connecting are available to you:
100
101- You can connect using the standard database methods :doc:`described
102 here <../database/connecting>`, either from within your
103 Controller class or your Model class.
104- You can tell the model loading function to auto-connect by passing
105 TRUE (boolean) via the third parameter, and connectivity settings, as
106 defined in your database config file will be used:
107 ::
108
109 $this->load->model('Model_name', '', TRUE);
110
111- You can manually pass database connectivity settings via the third
112 parameter:
113 ::
114
115 $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);
116
117