Fuel Documentation

Orm

Orm is short for Object Relational Mapper which does 2 things: it maps your database table rows to objects and it allows you to esteblish relations between those objects.
It follows closely the Active Record Pattern, but was also influenced by other systems.

Relating Models

Basic Usage

$_has_many = array('comments');

This example, if specified in Model_Article, enables fetching of an array of Model_Comment objects which have the field article_id matching the primary key of a given Article instance through the property comments.

Advanced Usage

$_has_many = array('comments' => array(
	'model_to' => 'Model_Fancy_Comment',
	'key_from' => 'article_id',
	'key_to' => 'parent_article_id',
	'cascade_save' => true,
	'cascade_delete' => false,
));

In the basic example, Orm automatically assumes the model name and field mapping. This example explicitly specifies the class name of the target model, the fields used to relate them, and whether actions performed on the current object should be cascaded to the target. It will return an array of Model_Fancy_Comment object where the comment's parent_article_id field corresponds to the current object's article_id. When saving an object the operation is also performed on its loaded relations, deleting isn't cascaded by default but can be if you switch this on.

Configuration Options

All of the configurations are optional when using the most common naming conventions.

Option Default Notes
model_to Calculated from alias If specified, must the the full class name of the target model (ex. Model_Comment).
By default, this value is formed by prepending 'Model_' to the singular form of the alias (ex. 'comments' becomes 'Model_Comment'), it is also expected to be in the same namespace as the current model.
key_from The key used for the relation in the current model (Usually id) Allows mapping the target model to an arbitrary field in the current model's table
key_to Calculated from the current model name By default, a relationship from a Model_Article to many Model_Comments would use the field article_id in the comments table
cascade_save bool true You can override these options at runtime by passing true as the first argument when calling save() or delete() on the originating model.
cascade_delete bool false

Has One

Specifies a one-to-one relationship to another model. The target model must include a "Belongs To" reference to the current model to allow the inverse relationship.

// Basic reference in an example Model_Book
$_has_one = array('author');

The author can then be referenced from a Model_Book instance as follows:

$author = $book->author;

Has Many

Specifies a one-to-many relationship to another model. The target model must include a "Belongs To" reference to the current model to allow the inverse relationship.

// Basic references in an example Model_User
$_has_many = array('posts', 'comments');

A user's posts can then be referenced by their respective aliases as follows:

// Returns an array of Model_Post objects
$posts = $user->posts;

// Returns an array of Model_Comment objects
$comments = $user->comments;