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 establish relations between those objects.
It follows closely the Active Record Pattern, but was also influenced by other systems.

Observers: Included observers

Included observers are listed below:

Observer_Self

While not exactly good practice, in some cases it's probably cleanest to just have the event method with your model. That's where the Observer_Self comes in, it checks if you model has a method named after the event and prefixed with _event_. For example for the after_save event you need to add a public method _event_after_save() to your model so it can have the observer call on the model itself.

// Just add the Observer
protected static $_observers = array('Orm\\Observer_Self');

// But if you only observe some events you can optimize by only adding those
protected static $_observers = array('Orm\\Observer_Self' => array('after_save', 'before_insert'));

Observer_CreatedAt

This observer acts only upon before_insert and expects your model to have a created_at property which will be set to the Unix timestamp when saving for the first time.

// Just add the Observer
protected static $_observers = array('Orm\\Observer_CreatedAt');

// Adding it with config:
// - only needs to run on before_insert
// - use mysql timestamp (uses UNIX timestamp by default)
// - use just "created" instead of "created_at"
protected static $_observers = array(
	'Orm\\Observer_CreatedAt' => array(
		'events' => array('before_insert'),
		'mysql_timestamp' => true,
		'property' => 'created',
	),
);

Observer_UpdatedAt

This observer acts only upon before_save and expects your model to have a updated_at property which will be set to the Unix timestamp when saving (also during the first time).

// Just add the Observer
protected static $_observers = array('Orm\\Observer_UpdatedAt');

// Adding it with config:
// - only needs to run on before_save
// - use mysql timestamp (uses UNIX timestamp by default)
// - use just "updated" instead of "updated_at"
protected static $_observers = array(
	'Orm\\Observer_UpdatedAt' => array(
		'events' => array('before_save'),
		'mysql_timestamp' => true,
		'property' => 'updated',
	),
);

Observer_Validation

This observer only acts on before_save. It is used to prevent the model from saving if validation rules fail. It uses the Fieldset class and can also generate the form for you.

The primary key(s) are not added to Validation nor to the Form as they're not editable and will be auto-increment most of the time. In cases where you set it upon creation and need validation you'll need to add the field manually.

The observer can be loaded as follows:

// Just add the Observer
protected static $_observers = array('Orm\\Observer_Validation');

// But adding it just for before_save is enough
protected static $_observers = array('Orm\\Observer_Validation' => array('before_save'));

Validation rules should be defined in your model in $_properties. This is demonstrated in Creating Models. After you have added the Validation Observer, the Orm\ValidationFailed exception will be thrown when the model's data fails to validate before save. As such, you must try/catch your calls to the save function of a model.

More extensive example functionality/scaffolding is shown below:

class Controller_Articles extends Controller
{
	public function action_create()
	{
		$view = View::forge('articles/create');
		if (Input::param() != array())
		{
			try
			{
				$article = Model_Article::forge();
				$article->name = Input::param('name');
				$article->url = Input::param('url');
				$article->save();
				Response::redirect('articles');
			}
			catch (Orm\ValidationFailed $e)
			{
				$view->set('errors', $e->getMessage(), false);
			}
		}
		return new Response($view);
	}

	public function action_edit($id = false)
	{
		if ( ! ($article = Model_Article::find($id))
		{
			throw new HttpNotFoundException();
		}

		$view = View::forge('articles/edit');
		if (Input::param() != array())
		{
			try
			{
				$article->name = Input::param('name');
				$article->url = Input::param('url');
				$article->save();
				Response::redirect('articles');
			}
			catch (Orm\ValidationFailed $e)
			{
				$view->set('errors', $e->getMessage(), false);
			}
		}
		return new Response($view);
	}

	public function action_delete($id = null)
	{
		if ( ! ($article = Model_Article::find($id))
		{
			throw new HttpNotFoundException();
		}
		else
		{
			$article->delete();
		}
		Response::redirect('articles');
	}

}

As this uses the Fieldset class to perform validation, it can also create forms for a model. In the following example, the create and edit forms are defined in a common view however you can just as easily define it in the model and obtain an instance of it in the view using Fieldset::instance().

// use an instance of Model_Article to create the form, you can also pass the classname
$fieldset = Fieldset::forge()->add_model($article);

// Populate the form with values from the model instance
// passing true will also make it use POST/PUT to repopulate after failed save
$fieldset->populate($article, true);

// The fieldset will be build as HTML when cast to string
echo $fieldset;

Observer_Typing

This is for 2 things: type enforcement for input and type casting for output from the DB. That means that when you're saving the Typing observer will try to cast the input value to the expected type and throw an exception when it can't. And when you're retrieving DB data, normally it would all be strings (even integers and floats) but with the typing observer those will be cast to their scalar type.

In addition to the above the Typing observer also adds support for serialized & json fields. Both should be string type fields ("text" preferably) but will have their value encoded for saving (using serialize() or json_encode()) and decoded when retrieving from the DB (using unserialize() or json_decode()).

The Observer_Typing isn't meant as an alternative to validation, don't try to use it as such. Neither are the exceptions thrown by this observer meant to be read by the visitor of your site, they're meant to help you debug your code.

// Just add the Observer
protected static $_observers = array('Orm\\Observer_Typing');

// But adding it just for these specific events is enough
protected static $_observers = array('Orm\\Observer_Typing' => array('before_save', 'after_save', 'after_load'));

For this observer to work you must have your the $_properties static variable set in your model, or not set at all using detection with DB::list_columns() (mySQL only!). When configuring it yourself the following settings are available:

Param Valid input Description
data_type varchar, int, integer, tinyint, smallint, mediumint, bigint, float, double, decimal, text, tinytext, mediumtext, longtext, enum, set, bool, boolean, serialize, json, time_unix, time_mysql The SQL data type, Required to have the typing observer used on a field.
null bool Whether null is allowed as a value
character_maximum_length int The maximum number of characters allowed for a string data type (varchar, text)
min int The minimal value for an integer
max int The maximum value for an integer
options array Array of valid string values for set or enum data type
Note: currently the options themselves cannot contain comma's.

Observer_Slug

This observer creates a unique url safe slug for your model. It works only upon before_insert and expects your model to have a title (to create the slug) and a slug (to save it) property.

// Just add the Observer
protected static $_observers = array('Orm\\Observer_Slug');

// With settings
protected static $_observers = array(
	'Orm\\Observer_Slug' => array(
		'events' => array('before_insert'),
		'source' => 'title',   // property used to base the slug on, may also be array of properties
		'property' => 'slug',  // property to set the slug on when empty
	),
);
protected static $_observers = array('Orm\\Observer_Slug' => array('before_insert'));

The Observer creates the slug from the title using Inflector::friendly_title() and adds an index, if the slug already exists.