Skip to content
World Wide Web Server edited this page Jul 4, 2012 · 45 revisions

This page details an implementation of Active Record for CodeIgniter that more accurately mimics the way Ruby on Rails works with models. It allows you to abstract large amounts of the searching and creation code out of your models, and can help with streamlining your controller code as well.

File:Activerecord.php.zip

[b]NOTE! Due to some of the code this technique uses, it will only work if you are running PHP5![/b]

[h3]Implementation[/h3]

To begin using the ActiveRecord class, download the most recent version and save it into your [b]/application/libraries[/b] folder. This file replaces the CodeIgniter Model class, and all your models will inherit from it.

Next, edit [b]/application/config/autoload.php[/b] and make sure that both the ActiveRecord and database libraries are automatically loaded when CodeIgniter runs:

[code] $autoload['libraries'] = array('activerecord', 'database'); [/code]

You are now ready to start using the ActiveRecord class in your application.

[h3]Convention Over Configuration[/h3]

Before we get onto actually using the class, let's talk a little about the approach Ruby on Rails takes to models and database design, and in particular its principle of "Convention over configuration".

When creating your database tables, there are certain conventions that the ActiveRecord class assumes you to be following. These are:

  • Every model maps to a database table
  • The table name will be the lowercase pluralised model name
  • Every table's unique key will be named [b]id[/b]
  • Relationships between tables will be handled by a separate relationship table, named for the two tables it connects, in alphabetical order, separated by an underscore ("_")

Some examples to clarify those three requirements:

  • I have a model named [b]Page[/b], so the table it maps to is named [b]pages[/b]
  • I have a model named [b]Person[/b], so the table it maps to is named [b]people[/b]
  • The [b]pages[/b] table contains fields: ** id ** title ** content
  • The [b]people[/b] table contains fields: ** id ** first_name ** last_name
  • Relationships between people and pages are held in a table named [b]pages_people[/b]
  • The [b]pages_people[/b] table contains fields: ** page_id ** person_id

Hopefully that all makes sense and seems fairly straightforward.

[h3]Creating A Model[/h3]

Continuing the example above, let's create our Page model:

[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Page extends ActiveRecord {

function __construct()
{
    parent::ActiveRecord();
    $this->_class_name = strtolower(get_class($this));
    $this->_table = $this->_class_name . 's';
    $this->_columns = $this->discover_table_columns();
}

}

?> [/code]

This is the basic template for all models using ActiveRecord - and, in many cases, that is all you will need to write in the model file! When the object is instantiated, it stores some meta information about itself: its class name, table name, and the columns that exist in its table.

Models for non-standard plurals are only slightly different - here's our Person model:

[code] <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Person extends ActiveRecord {

function __construct()
{
    parent::ActiveRecord();
    $this->_class_name = strtolower(get_class($this));
    $this->_table = 'people';
    $this->_columns = $this->discover_table_columns();
}

}

?> [/code]

The only difference is that we need to hardcode the table name instead of extrapolating it from the class name.

PAGE UNDER CONSTRUCTION

Clone this wiki locally