1 <html xmlns=
"http://www.w3.org/1999/xhtml" xml:
lang=
"en">
3 <title>CarbonPHP Documentation
</title>
4 <style type=
"text/css">
11 font-family: "Lucida Sans Unicode", "Lucida Grande";
27 margin: 10px 0 10px 0;
28 list-style-position: inside
;
36 border: 1px dotted
#444;
44 border: 1px solid
#ccc;
56 <p>You are not required to use models in your web application. They can be used for those people who wish
57 to use a more traditional model-view-controller approach.
</p>
61 <h1>What is a Model?
</h1>
63 <p>Models are classes that are designed to handle information in your databases. An example is if you
64 create a blog application using CarbonPHP. You may have a model class that has methods to insert, update,
65 and retrieve blog data.
</p>
67 <p>An example of this model may look like the following.
</p>
72 class Blogmodel extends Model
<br />
74 public $title = '';
<br />
75 public $content = '';
<br />
76 public $data = '';
<br />
78 public function __construct()
<br />
79 {<br />
80 parent::__construct();
<br />
81 }<br />
83 public function get_last_ten_entries()
<br />
84 {<br />
85 $query = $this-
>db-
>get('entries',
10);
<br />
86 return $query-
>result();
<br />
87 }<br />
89 public function insert()
<br />
90 {<br />
91 $this-
>title = $_POST['title'];
<br />
92 $this-
>content = $_POST['content'];
<br />
93 $this-
>date = time();
<br />
95 $this-
>db-
>insert('entries', $this);
<br />
96 }<br />
98 public function update()
<br />
99 {<br />
100 $this-
>title = $_POST['title'];
<br />
101 $this-
>content = $_POST['content'];
<br />
102 $this-
>date = time();
<br />
104 $this-
>db-
>update('entries', $this, array('id', $_POST['id']));
<br />
105 }<br />
111 <p><i>Note
</i>: the above example is using methods from the
<b>Active Record
</b> data class.
</p>
114 <div class=
"content">
115 <h1>The Anatomy of a Model
</h1>
117 <p>Models are stored the in
<b>application/models/
</b> directory. They can be stored in subdirectories for
118 people who wish to use that type of organisation.
</p>
120 <div class=
"example">
123 class Model_name extends Model
<br />
125 public function __construct()
<br />
126 {<br />
127 parent::__construct();
<br />
128 }<br />
134 <p><b>Model_name
</b> is the name of your class. Class names must be capitalised. You must also make sure
135 that the model class extends the base
<b>Model
</b> class. The file name will be a lower case version of
138 <div class=
"example">
141 class User_model extends Model
<br />
143 public function __construct()
<br />
144 {<br />
145 parent::__construct();
<br />
146 }<br />
152 <p>The model file name will be the following.
</p>
154 <div class=
"example">
155 application/models/
<b>user_model.php
</b>
159 <div class=
"content">
160 <h1>Loading a Model
</h1>
162 <p>Usually your models will be loaded and used from within your controllers methods.
</p>
164 <div class=
"example">
165 $this-
>load-
>model('
<b>Model_name
</b>');
168 <p>If you have organised your models in subdirectories you will need to include the directory in the
171 <div class=
"example">
172 $this-
>load-
>model('
<b>Directory_name
</b>/
<b>Model_name
</b>');
175 <p>Once your model has been loaded you will access the models methods using an object with the same
176 name as the model class you loaded.
</p>
178 <div class=
"example">
179 $this-
>load-
>mode('
<b>Model_name
</b>');
<br />
180 $this-
><b>Model_name
</b>-
>method();
183 <p>You can specify a different name for the model object in the second parameter of the loading method.
</p>
185 <div class=
"example">
186 $this-
>load-
>model('
<b>Model_name
</b>', '
<b>foo
</b>');
<br />
187 $this-
><b>foo
</b>-
>method();
190 <p>An example of a controller that loads and uses a model, then loads a view is shown below.
</p>
192 <div class=
"example">
195 class Blog extends Controller
<br />
197 public function recent()
<br />
198 {<br />
199 $this-
>load-
>model('Blog');
<br />
200 $data['query'] = $this-
>blog-
>get_last_ten_entries();
<br />
201 $this-
>load-
>view('blog_view', $data);
<br />
202 }<br />
209 <div class=
"content">
210 <h1>Using a Database in Models
</h1>
212 <p>Unless you autoload the database library (this is explained later) you model will not automaticall connect
213 to your database. There are three methods you can use for connecting to the database in your models.
</p>
216 <li>Connect using the standard database methods, either in your controller or model class.
</li>
217 <li>You can tell the model loading method to connect to the database by passing
<b>true
</b> via
218 the third parameter, and settings for your database defined in the configuration file
219 <b>config/database.php
</b>.
</li>
221 <div class=
"example">
222 $this-
>load-
>model('
<b>Model_name
</b>', '', true);
225 <li>You can manually the pass the database connection settings via the third parameter.
</li>
227 <div class=
"example">
228 $config['hostname'] = 'localhost';
<br />
229 $config['username'] = 'dbuser';
<br />
230 $config['passwprd'] = 'dbpassword';
<br />
231 $config['database'] = 'databasename';
<br />
232 $config['dbdriver'] = 'mysql';
<br />
233 $config['dbprefix'] = '';
<br />
234 $config['pconnect'] = false;
<br />
235 $config['db_debug'] = true;
<br />
236 $config['active_r'] = true;
<br />
238 $this-
>load-
>model('
<b>Model_name
</b>', '', $config);