Models

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

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 Codingox 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 Base_Model {

        public function __construct()
        {
                parent::__construct();
                $this->database();
        }

        public function insert()
        {
                $data = array(
                  'title'   => $_POST['title'],
                  'content' => $_POST['content'],
                  'date'    => time()
                );

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

        public function update()
        {
                $data = array(
                  'title'   => $_POST['title'],
                  'content' => $_POST['content'],
                  'date'    => time()
                );

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

}

Note

The methods in the above example use the Query Builder database methods.

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 Form Library $this->form->post('title').

Anatomy of a Model

Model classes are stored in your models/ directory.

The basic prototype for a model class is this:

class Model_name extends Base_Model {

}

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 must match the class name. For example, if this is your class:

class User_model extends Base_Model {

}

Your file will be this:

models/User_model.php

Loading a Model

Your models will typically be loaded and called from within your controller methods. To load a model you will use the following method:

$this->model('model_name');

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

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

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

$this->model('model_name');

$this->model_name->method();

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

class Blog_controller extends Base_Controller {

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

                $this->view('blogView');
        }
}

Connecting to your Database

When a model is loaded it does NOT connect automatically to your database.

You can connect using the standard database methods described here, either from within your Controller class or your Model class.