In the meantime you can check the manual by downloading IgnitedRecord here.
This manual is for 0.1.1 .
This is an ActiveRecord-like (Ruby's ActiveRecord) ORM model which is aimed to be simple to use, yet very customizable.
Every derived class is a factory which produces objects which in turn represents a row in the database table which the derived class is mapped to.
Drop the folder ignitedrecord in your application/models directory.
IgnitedRecord uses the CodeIgniter ActiveRecord implementation, so the $active_record option must be set to true in the application/config/database.php.
The MY_Loader.php file is optional, but provides a custom loader function which simplifies the loading of IgnitedRecord and its derived classes.
This file should be put in the application/libraries folder. If you already have a custom loader file (or use another loader class, eg. Matchbox), copy the ORM you also have to install a yamlparser.
yamlfactory() uses a yaml parser called YAYparser, it is bundled with IgnitedRecord, but you can easily edit yamlfactory() to utilize another parser.
To install YAYparser, copy yayparser_helper.php from the helpers dir to your application/helpers
If you are using PHP 4, the two __call() methods won't work. The __call() methods only provides an aggregation of the helpers and behaviours, which is not necessary, but it makes for a bit shorter code.
Example:
Another thing you might want to do is to replace all '= new' with '=& new' (8 places in the IgnitedRecord class).
This model is meant to be extended by child classes, and is not to be used directly. Therefore you have to let other classes extend the IgnitedRecord class.
Because the models inherit each other, you have to load them in order. The Model class must be loaded before the IgnitedRecord class which in turn must be loaded before the derived classes.
The easiest way is to call $this->load->model('ignitedrecord/ignitedrecord') in the controller, before you load any of the derived classes.
There is also a custom MY_Loader.php which simplifies the loading of derived classes.
It provides this method:
If modelname is not set (is false), orm() will not call Loader::model(). It will only load the Model class and IgnitedRecord class, but will not instantiate them.
But if modelname is set, after it has loaded the Model class and IgnitedRecord, orm() will call Loader::model() and pass all recieved parameters to it (well, modelname, name and db_conn).
So the orm() method can act like a wrapper for the Loader::model().
Example:
$this->load->orm();The most simple model you can have with IgnitedRecord is the following:
class Page extends IgnitedRecord{ This model maps itself to the Pages table in the database and contains all basic methods for fetching data from the database.
See Methods for fetching data for more information on how to fetch data using the inherited methods.
In the child classes you can specify a lot of different options, if an option is not specified a default value is assumed:
| Property | Default | Description |
|---|---|---|
| $table | Pluralized version of class name ( minus 'model', if exists) |
The table this model is mapped to |
| $id_col | id | The column that stores the unique id, preferred to be auto incremental unsigned int |
| $columns | autoloaded when needed | Contains all column names, define yourself to save one query (SHOW COLUMNS FROM…) |
| $child_class | IgnitedRecord_record | The name of the class which this factory produces |
| $auto_load_relationships | false | If to autoload the relations when an object is created (Warning, can cause endless loops because of a related table having $auto_load_relationships switched on and relates back to this table) |
| $act_as | array() | The behaviours this model shall utilize, read more about Behaviours |
| $belongs_to | null | Specifies the attributes of one or more Belongs To relationships that this model has |
| $has_many | null | Specifies the attributes of one or more Has Many relationships that this model has |
| $has_one | null | Specifies the attributes of one or more Has One relationships that this model has |
| $has_and_belongs_to_many | null | Specifies the attributes of one or more Has And Belongs To Many relationships that this model has |
| $habtm | null | An alias for $has_and_belongs_to_many. These two arrays will be merged to one in the constructor. |
There are also a second way of creating a derived class: The factory() and yamlfactory() methods:
This method creates and instantiates a derived class linked to the table table name and with the properties stored in the settings array (property => value).
The instantiated object is returned.
Note: All propertynames is prepended with a double underscore.
A factory that utilizes yaml instead of a PHP array.
If the filename is a yaml string (contains \n) it is parsed with yayparser into a PHP array. This array is then passed to a new IgnitedRecord object.
If the filename is a filename (does not contain \n), IgnitedRecord attempts to load the file, then parses it into a PHP array.
Note: All propertynames is prepended with a double underscore.
To define a relationship between two tables, you can use two approaches:
You have to set one of these properties:
Or you can use one of these methods (they accept the same data as the properties, but can only add/replace relations):
The simplest value these properties can contain is the table name of the related table.
But there are more settings that can be altered and specified through these properties. The settings and their default values vary, so check the part of the manual that is covering the specific relation type.
The properties can be specified in four ways:
// single:Example:
var $belongs_to = array('users','forums'); // relate to two tablesThere are four types of relations which IgnitedRecord supports:
The Belongs To relationship is often used at the receiving end of a Has Many or a Has One relationship.
Example:
| class user extends IgnitedRecord{ var $has_one = ‘pictures’; function user(){ parent::IgnitedRecord(); } } class picture extends IgnitedRecord{ var $belongs_to = 'users'; function picture(){ parent::IgnitedRecord(); } } |
![]() |
Here we have specified a relation between the users table and the pictures table.
The pictures Belongs To users, and therefore pictures table has a foreign key column (the key referring to the other table) referring to users (called users
To specify special settings in a relationship, define an array containing the settings you want to override (would replace the normal position of the table name).
The default settings for the Belongs To relation is the following:
| Key | Default | Description |
|---|---|---|
| table | REQUIRED | The related table |
| name | modelname (of the related table) or singular(tablename) | The relation name, used when adding, removing and loading related records |
| model | tablename or singular(tablename), depending on if a model with that name exists | The model to be used when instantiating related records (if no model is found, it uses this class to instantiate the records) |
| col | tablename of the related table + id | The foreign key column used by the relation |
This relation type describes a one to many relationship.
Example:
A user can have many posts:

Here Foo Bar has two posts, and Another Person has one post. The posts still belong to only one user, however.
Code for establishing this relation:
class user extends IgnitedRecord{Settings for a Has Many relationship:
| Key | Default | Description |
|---|---|---|
| table | REQUIRED | The related table |
| name | the tablename of the related table | The relation name, used when adding, removing and loading related records |
| model | tablename or singular(relatedtablename), depending on if a model with that name exists | The model to be used when instantiating related records (if no model is found, it uses this class to instantiate the records) |
| col | tablename of this table + id | The foreign key column used by the relation |
This is like an inverted Belongs To relation, both relates one to one. But by specifying a Has relationship, we declare that the foreign key (the key referring to the other table) should reside in the other table. In the case in the Belongs To paragraph, pictures.
Or you can look at it as a Has Many relation, but with the limitation that only one related record is allowed.
Settings for a Has One relationship:
| Key | Default | Description |
|---|---|---|
| table | REQUIRED | The related table |
| name | modelname (of the related table) or singular(relatedtablename) | The relation name, used when adding, removing and loading related records |
| model | tablename or singular(tablename), depending on if a model with that name exists | The model to be used when instantiating related records (if no model is found, it uses this class to instantiate the records) |
| col | tablename of this table + id | The foreign key column used by the relation |
This is the relation you choose when you need to have many to many relations. For example a developer can have (or belong to) many clients, but a client can have (or belong to) many developers.
Example table diagram:
class person extends IgnitedRecord{Example of data inserted and with relations in both ways:

Here the Another Person relates to both img1 and img3, while img2 relates to both Foo Bar and Some Dude.
Example returns when calling get_related():
Settings, their default values and their keynames:
| Key | Default | Description |
|---|---|---|
| table | REQUIRED | The related table |
| name | the related tablename | The relation name, used when adding, removing and loading related records |
| model | tablename or singular(relatedtablename), depending on if a model with that name exists | The model to be used when instantiating related records (if no model is found, it uses this class to instantiate the records) |
| rel_table | 'thistablename_relatedtablename' or 'relatedtablename_thistablename', depending on which one comes first alphabetically | The table used for storing the relations |
| thiscol | tablename + id | The foreign key column used relating to this table |
| othercol | tablename of the related table + id | The foreign key column used relating to the related table |
All these methods return IgnitedRecord_records, see The IgnitedRecord_record object for information on how to obtain and manipulate data from them.
If the $
The find() method returns the record with the supplied id id.
Returns false if nothing was found.
This method fetches one record from the table. It is meant to be used in conjunction with CodeIgniter's ActiveRecord class, where the ActiveRecord class filters and sorts the query (you can see it as a replacement for the Active Record's get() method).
The only call to the db this method does is the following (to fetch the row from the mapped table):
$data = $this->db->get($this->
Returns the first record where the data in the column(s) column(s) matches data.
The column(s) parameter can be array or scalar, the same for data. These two arrays are then merged into an array, where the elements in the column(s) array is the keys and the elements in the data array are values. This array are then sent to CodeIgniter's ActiveRecord library, so the column(s) can contain !=, >, <, etc.
Example:
$record = $this->page->find_by('title','index'); // searches for a record with the title 'index'Returns a record (the first one, so limit the query to return one row to lower bandwith usage) created from the data returned from the query sql_query_string. The query should be escaped and should start with 'SELECT * ' or equivalent.
Example:
$record = $this->page->find_by_sql('SELECT , count() AS Authors FROM pages WHERE Authors > 2 LIMIT 1');Returns all records in the table.
Or, it could be used as a substitute for CodeIgniter's ActiveRecord db::get() method. The only call to the db this method does is the following (to fetch all rows from the mapped table):
$data = $this->db->get($this->
The difference is that find_all() returns all rows, get() returns only one.
As find_by(), but returns all matching records.
As find_by_sql(), but returns all matching records.
Returns a new IgnitedRecord_record which isn't linked to a db row. Populates it with the associative array data.
To enter the record in the database after the creation, use the save() method.
This is the object that stores the individual row data. It is linked to the IgnitedRecord model (or rather the derived class) which it uses for saving, updating, relations, etc.
The properties of the IgnitedRecord_record are the column values of the record.
Example:
The methods available in the IgnitedRecord_record class are these:
This method saves the record to the database. If the record does not exist in the database (the id is set to null (default)), an insert is performed.
Empty objects cannot be inserted (because CodeIgniter does not allow inserts without data).
If force is true, the object will be saved even if it was unchanged.
$rec = $this->page->new_record(); If the INSERT/UPDATE is successful, true is returned, otherwise is false returned.
If the data in the record has not been altered, save() will not save the data again (and false is returned).
Updates the data in the current object. If the row has been deleted from the database, the object still contains the data (but the id is removed, so it isn't linked to a record).
Returns true if this object is linked to a row in the database.
$rec = $this->page->find(3); Returns the unique id of the current record (the value of the id column(s)).
If this record does not exist in the database, false is returned.
Deletes the record from the database. All data (except relations and id) in the record are preserved, so you can save it again or use the data in any other way.
Loads the related records of this object. The name specifies a certain relation to load relations for. If the name parameter is omitted, all relations will be loaded.
The relations will be stored in the property with the name of the relation if it is not set (not used). Otherwise you can fetch the related objects using get_related().
$record->load_rel();Loads the related records for the relation with the name name. Alias for load_rel().
Returns the related records from the relation with the name name. Depending on if it is a Belongs To, Has Many, Has One or Has And Belongs To Many, the result can be an array or an IgnitedRecord_record. If the relation is not loaded, it will be loaded.
If no related object(s) are found, false will be returned.
This method establishes a relationship between this IgnitedRecord_record and the supplied record. The relationship_name is the name of the desired relationship to use, if omitted IgnitedRecord will try to figure it out.
Depending on the settings of the IgnitedRecord instance tied to this object, the type of relation can vary.
Ex. If you have defined two relations, one to "posts" with Has Many and one to "moderators" with Has And Belongs To Many, and you pass an object of the type "posts", a Has Many relation will be established.
If no relation is established with this record's table and record's table in the Ignitedrecord instance (the model attached to this record), no relation will be formed.
Somewhat of a shorthand for add_relationship, but requires the relationship name.
$obj->add('child',$object);If a relation has been established between this record and record, it will be removed. The relation_name is the name of the specific relation to remove, if omitted IgnitedRecord will try to figure it out. If no relation is defined (in the model attached to this record) or established betwen the two records, nothing is done.
Shorthand for remove_relationship(), but requires the relationship name.
Coming soon
I haven't made this section yet, but if you want to check out on how to make a behaviour, look at $
act_as, $_child_class_helpers and load_behaviours() in IgnitedRecord. The hooks are places where you can add your own code to execute between different parts of IgnitedRecord.
Trigger is maybe a more fitting description, but you have a hook and you attach your code to the hook (sort of hanging your code there and then when the hook is called, the code on the hook will be called :P ).
To add some code to a hook you first have to place the code in a separate function/method which you then can add to the hook.
The second step is to register your function with the help of the add_hook() method:
add_hook('hook_name', 'function_name', priority = 10)
Method variant:
add_hook('hook_name', array(object, 'method_name'), priority = 10)
The higher the priority number, the lower priority the hooked code will have. The hooked code will be run in order with the lowest priority number first (and if two hooks have the same priority, the one added first will be run first).
Example:
function add_prop(&$object){ // the ampersand is important, it is required for the reference to work (at least for scalars, depending on PHP version)Here are the hooks, their names, arguments and description:
| Hook | Arguments | Description |
|---|---|---|
| pre_get | This hook is run before the get() method does anything | |
| post_get |
|
This hook is run after the get() method has fetched and instantiated data |
| pre_find |
|
This hook is run before the find() method does anything |
| post_find |
|
This hook is run after the find() method has fetched and instantiated data |
| pre_find_by |
|
This hook is run before the find_by() method does anything |
| post_find_by |
|
This hook is run after the find_by() method has fetched and instantiated data |
| pre_find_by_sql |
|
This hook is run before the find_by_sql() method does anything |
| post_find_by_sql |
|
This hook is run after the find_by_sql() method has fetched and instantiated data |
| pre_find_all | This hook is run before the find_all() method does anything | |
| post_find_all |
|
This hook is run after the find_all() method has fetched and instantiated data |
| pre_find_all_by |
|
This hook is run before the find_all_by() method does anything |
| post_find_all_by |
|
This hook is run after the find_all_by() method has fetched and instantiated data |
| pre_find_all_by_sql |
|
This hook is run before the find_all_by_sql() method does anything |
| post_find_all_by_sql |
|
This hook is run after the find_all_by_sql() method has fetched and instantiated data |
| post_new_record |
|
This hook is run after the new_record() method has instantiated data |
| save_pre_stripdata |
|
This hook is run before the strip_data() method has returned a clean array |
| save_post_stripdata |
|
This hook is run after the strip_data() method has returned a clean array |
| save_pre_insert |
|
This hook is run before the save() method has called db::insert() |
| save_post_insert |
|
This hook is run after the db::insert() has inserted the clean data array |
| save_pre_update |
|
This hook is run before the save() method has called db::update() |
| save_post_update |
|
This hook is run after the db::update() has saved the clean data array |
| save_abort |
|
This hook is run if the save() method detect that the object is unchanged or empty and aborts |
| post_save |
|
This hook is run after the save() method has saved the clean data array |
| pre_update |
|
This hook is run before the update() has created the new refreshed object |
| post_update |
|
This hook is run after the update() method has loaded and instantiated the object |
| pre_delete |
|
This hook is run before the delete() has done anything |
| pre_delete_query |
|
This hook is run after the delete() method has removed all relations from the object to be deleted |
| post_deleted |
|
This hook is run after the delete() method has deleted the object |
This is a version of print_r that accepts an ignore array: (Found on php.net)
/ The recommended ignore array is this:
array('
Copyright © 2008, Martin Wernstahl
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
Release Date: June 26, 2008
Release Date: May 30, 2008
Release Date: May 3, 2008
Release Date: May 1, 2008
Release Date: April 30, 2008
Release Date: April 5, 2008