Models

What is a model?

Whenever data needs to be retrieved, manipulated or deleted this should always be done by a model. A model is a representation of some kind of data and has the methods to change them. For example: you never put SQL queries in a controller, those are put in the model and the controller will call upon the model to execute the queries. This way if your database changes you won't need to change all your controllers but just the model that acts upon it.

Creating models

In order to function, a model must do the following:

  • Reside in "application/models" folder (or a sub-folder).
  • File name must match the class name exactly, e.g. products.php
  • The class name must map to the file name (with / replaced with _) and each word is capitalized.
  • Must have the Model class as a (grand)parent.

Some examples of model names and file locations:

// application/models/foobar.php
class Foobar_Model extends Model {
 
// application/models/products.php
class Products_Model extends Model {

Models can be in sub-folders:

// application/models/users/settings.php
class Users_Settings_Model extends Model {
 
// application/models/products/categories.php
class Products_Categories_Model extends Model {

Models can extend other models:

// application/models/categories.php
class Categories_Model extends Store_Model {
 
// application/models/products.php
class Products_Model extends Categories_Model {

Example model

Typical model would have the code to add, edit, delete records from the database. Here is an example of what such a model class might look like:

class Products_Model extends Model
{
	public function add($data)
	{
		$productID = $this->db->insert('products', $data);
		return $productID;
	}

	public function update($productID, $data)
	{
		$retval = $this->db->update('products', $data, array('product_id' => $productID), 1);
		return $retval;
	}

	public function get($productID)
	{
		$product = $this->db->query("SELECT * FROM `:prefix:products` WHERE `product_id`=? LIMIT 1", array($productID))->row();
		return $product;
	}

	public function delete($productID)
	{
		$retval = $this->db->delete('products', array('product_id' => $productID), 1);
		return $retval;
	}
}

Loading models

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

loader::model('products');

You may now access its methods in the controller like this (notice the "_model" suffix that is added to "products" when calling model's method):

$this->products_model->get(12);

You may pass additional variables to your model's constructor if necessary when loading it:

class Products_Model extends Model
{
	private $config = array();
	public function __construct($config = array())
	{
		parent::__construct();
		$this->config = $config;
	}
}
// In the controller we'd have this:
loader::model('products', array('config' => $config));

You may also assign a different name for it to be used in controller:

loader::model('products', array(), 'store_model');
$this->store_model->get(12);

You may also assign your model class directly to a variable:

$products = loader::model('products', array(), null);
$products->get(12);

If your model is located in a sub-folder then upon loading it system would use the file name of the model by default as its name. For example this model:

// application/models/store/products.php
class Store_Products_Model extends Model {

Upon loading it, we'll be able to refer to it like this:

loader::model('store/products');
$this->products_model->get(12);

You may always specify the name manually if you prefer:

loader::model('store/products', array(), 'store_products_model');
$this->store_products_model->get(12);