NAME

Norma::ORM::Mappable - A Moose role to map database tables to objects

SYNOPSIS

  package MyApp::Recipe;
  use Moose;

  with 'Norma::ORM::Mappable' => {
	dbh => $dbh,
	table_name => 'recipes',
  };

  1;

  package main;

  my $recipe = MyApp::Recipe->new(
  	title => 'Scrambled Eggs',
  	instructions => 'Break two eggs into a bowl...',
  );
  $recipe->save;

ROLE PARAMETERS

dbh => $dbh

A database handle from DBI->connect

table_name => $table_name

The name of the table which should map to this object

key_field_names => [$primary_key_name, ...] (optional)

A list of column names that should be seen as valid for unique lookups

relationships => [ { name => $name, class => $class, nature => $nature } ] (optional)

An arrayref of hashrefs, each hashref specifying a name, class, and nature. The name will be used to create an accessor method on this object. The class should be the class name of another object with Norma::ORM::Mappable role. The nature is one of belongs_to, has_many, or has_one. You may also specify foreign_key and foreign_primary_key as your naming scheme requires. For example, our recipe might have tags and comments:

  with 'Neocracy::ORM::Table' => {
  	...
	table_name => 'recipes',
  	relationships => [ 
  		{
			name   => 'comments',
  			class  => 'MyApp::Recipe::Comment',
			nature => 'has_many',
		}, {
			name   => 'contributors',
  			class  => 'MyApp::Recipe::Contributor',
			nature => 'belongs_to',
		}, {
			name      => 'ingredients',
			class     => 'MyApp::Recipe::Ingredient',
			nature    => 'has_many',
			map_table => 'recipe_ingredients_map',
			foreign_key         => 'ingredient_id',
			foreign_primary_key => 'recipe_id',
		}
	];

Objects and collections loaded through these relationships will be loaded lazily.

METHODS PROVIDED BY THIS ROLE

new(...)

Instantiate an object in preparation for inserting a new row with save() or merge(). Use load() to instatiate an object from an existing row in the database.

load(id => $primary_key_id)

Class method to instantiate an object from an existing row in the database.

save

Write the object to the database, either through an insert or an updated, depending on whether the object was instantiated via new() or load().

merge

Write to the database if the row passes any unique constraints, otherwise instantiate from the already-existing row.

validate

Perform subtype type checking to see that values pass attribute-level constraints.

delete

Delete from the database the row that corresponds to this object.

collect(where => { $column => $value }, ...)

Class method to return a collection of objects. See Norma::ORM::Collection for details.

AUTHOR

David Chester <davidchester@gmx.net>

COPYRIGHT AND LICENSE

This software is copyright (c) 2010 by David Chester.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.