The idea behind a model is to give the user a database-independent way of defining how the data looks alike and how different parts of the data relate to each other.
In database terms,
you might think of a schema definition.

Besides the pure definition of a model,
creation,
updating and lookup of data are also possible in a comfortable way.

Every model consists of two classes: AppName::Model::ModelName and AppName::Model::ModelName::Schema.
Behind the scenes,
a class named AppName::Model::ModelNameCollection is created by Jifty::ClassLoader.

allow to work with multiple records (referred to as Collections) without effort

To get all these things done, Jifty allows one to describe the schema definition in a simply comprehensible but powerful syntax that looks more like written text than a programming language. The schema definition is made inside the MyApp::Model::XXX::Schema package and every single column to get created starts with the word column followed by the column's name.

A simple definition could look like this:

column name =>
type is 'text',
label is 'Name',
render as 'Text',
since '0.0.1';

The following BNF shows the full syntax supported (omitting non-terminals that are self-explanatory to perl-developers):

Every time you run the jifty utility with schema as an argument, Jifty will keep track on what it has done for you. To get that done, the version-number being stored in your application's config file etc/config.yml under the key named framework/Database/Verson is matched against your schema definition.

To force an update of your schema, simple create a new version number in your config file and modify your schema definition by using exactly this version number for every modified entry. After running

jifty schema --setup

your database structure will be in sync to your schema definition. See Jifty::Manual::Upgrading for more information on model upgrading.

This is the model-class you created to access individual records of your desired type. You will directly deal with objects of this class.

MyApp::Record

All records of MyApp::Model::Xxx will have this class as their base class. Usually, this class will be automatically created by Jifty::ClassLoader for you. But, if you want to automatically enable all your records to do something, you will have a chance to do so by manually creating this class.

This is the lowest-level class that the database stack provides. It directly deals with the underlying database.

App::Model::XxxCollection

As the name applies, a collection is a set of typically more than one record. Every collection of this class consists of multiple App::Model::Xxx objects that can get retrieved from your data-store without explicit SQL statements, ordered by any criteria you give, paged in the fashion you like, and iterated sequentially or accessed at random order.

App::Collection

Every collection of your schemata will have this class as its base. Usually this class is automatically created by Jifty::ClassLoader. If you intend to create new features for all of your collection this will be your chance to do.

Working with a single record means working with objects of classes like MyApp::Model::Xxx. The typical creation and usage of a single record is:

# create an object to allow data access
my $object = new MyApp::Model::Xxx;
# either create a representation in the DB
$object->create(column => 'value', ...);
# or load the data from DB somehow
$object->load($id); # by a matching ID
$object->load_by_cols(column => 'value', other_column => 'secondvalue');
# try to load and if failed, create a record
$object->load_or_create(column => 'value');
# get the record's ID in the database
# results in 'undef' if record is not valid (which usually means not found)
my $id = $object->id;
# delete the record from the database
$object->delete;

To access data stored in different columns of a record you may use some of the automagically created methods on the object:

# read some column named 'colname'
my $value = $object->colname;
# write some value to a column named 'colname'
$object->set_colname($value);
# get all columns in a single hash (not a reference!)
my %record = $object->as_hash;

Especially, when writing to a record, you need not worry about how to write back the data to the database, the object will manage this step on its own.

Working with more than one record of the same object-class brings collections into the game. Usually, a collection you deal with is of a type that conforms to your model name, MyApp::Model::XxxCollection and usually holds records of class MyApp::Model::Xxx. You typically use a collection like this:

# create a collection object
my $collection = new MyApp::Model::XxxCollection;
# get all items of the model into the collection
$collection->unlimit;
# or restrict items to match some condition
$collection->limit(column => 'colname', operator => '=', value => 42);
# bring the items into some sorting order
$collection->order_by(column => 'colname');
# if neccesarry, directly jump to some record from the set
$collection->goto_first_item;
$collection->goto_item(42);
# iterate through the result set
while (my $record = $collection->next) {
# do something with $record
}
# directly access the first or last item
# be careful: this will set the current position also!
my $first = $collection->first;
my $last = $collection->last;
# get back an array-ref containing all items
my $records = $collection->items_array_ref;

In order to construct more complex restrictions the limit method may get called more than once, specifying one single condition with each call.

Every use of limit constructs either a clause or a subclause. A subclause is built either if the subclause attribute is used or a column is used repeatedly.

Every clause is built up by combining its subclauses (if any) using the entry_aggregator operator (whose default is OR) as a combining operator. Clauses are then ANDed together to yield the final restriction that is finally used to retrieve the records in question.

The operator (whose default is '=') can be any legal SQL operator like =, <=, >=, !=, LIKE, IS, IS NOT as well as some convenience operators that silently use LIKE with properly set wildcards (MATCHES, STARTSWITH or ENDSWITH).

When writing templates you often simply access some record from a model and want to operate on this very record by modifying it or you might want to add a new record of some type. To do this, our faithful Jifty::ClassLoader will create classes named MyApp::Action::CreateXxx, MyApp::Action::UpdateXxx or MyApp::Action::DeleteXxx for you. This enables you to write a template to operate on a single record like this:

The elegant thing around here is that you could write the class name of your action-class simply as UpdateXxx instead of the full package name MyApp::Action::UpdateXxx and there is no need to write a repeating update procedure for every record class that comes along. DRY - don't repeat yourself :-)