Depth-Studios
Welcome guest (Login / Register)
Models
We have defined a Model as follows, a model is used as a layer between the controller/view and the database. It is also used to parse data from 1 structure to another.

You can for example use models to extend the models which are created by the ORM, or use models to parse data from the database to XML.

The layout of a model class looks very much like a controller except that you extend derive from the Model class and the name of the class has to end with Model.
  1. <?php
  2.  
  3. class UbbModel extends Model
  4. {
  5. public function parse($text)
  6. {
  7. $text = str_replace('[b]', '<b>', $text);
  8. $text = str_replace('[/b]', '</b>', $text);
  9.  
  10. return $text;
  11. }
  12. }
  13.  
  14. ?>


As you can see, we use the above model to parse the text and apply some html tags.

Loading Models
When you want to load our ubb model in the blog controller we made earlier, we can use the function $load->model() from the controller as shown below.
  1. <?php
  2.  
  3. class BlogController extends Controller
  4. {
  5. public function view()
  6. {
  7. print $this->load->model('ubb')->parse('[b]Hello World![/b]');
  8. }
  9. }
  10.  
  11. ?>


Now you will see the bold text 'Hello World!'. When you load a model, the system will save the reference to it so it is only loaded once.