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.

Relations: 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.

Example

Let's say we have a model Model_Post and it has many Model_Comments (which in turn belong to the post). The ID of the Model_Post is saved with the Model_Comment instance in its own table. This means the comments table will have a column post_id (or something else you configure), while the posts table won't mention the comments. If you keep to the defaults all you need to do is add 'comments' to the $_has_many static property of the Model_User:

protected static $_has_many = array('comments'));

Full config example with defaults as values

// in a Model_Post which has many comments
protected static $_has_many = array(
	'comments' => array(
		'key_from' => 'id',
		'model_to' => 'Model_Comment',
		'key_to' => 'post_id',
		'cascade_save' => true,
		'cascade_delete' => false,
	)
);