The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

Model::Envoy::Storage::DBIC

A Moose Role that adds a DBIx::Class persistence layer to your Moose class

Traits

This role implements one trait:

DBIC

Marking an attribute on your object with the 'DBIC' trait tells this role that it is backed by a DBIx::Class ResultClass column of the same name. It also allows for a few custom options you can apply to that attribute:

primary_key => 1

Indicates that this attribute corresponds to the primary key for the database record

rel => 'rel_type'

Indicates that the attribute is a relationship to another model or a list of models. Possible values for this option are

belongs_to
has_many
many_to_many
mm_rel => 'bridge_name'

For many-to-many relationships it is necessary to indicate what class provides the linkage between the two ends of the relationship ( the linking class maps to the join table in the database).

Required Methods

There are two methods you will need to implement in a class that uses this role:

dbic()

This should return the name of the DBIx::Class ResultClass that your Model uses for database storage.

_schema()

This must return a DBIx::Class schema object for other methods to use when communicating with your database.

Methods

new_from_db( $dbic_result, [$no_rel] )

Takes a DBIx::Class result object and, if it's class matches your class's dbic() method, attempts to build a new instance of your class based on the $dbic_result passed in.

The `no_rel` boolean option prevents the creation process from traversing attributes marked as relationships, minimizing the amount of data pulled from the database and the number of new class instances created.

Returns the class instance if successful.

db_save()

Performs either an insert or an update for the model, depending on whether there is already a record for it in the database.

Returns the calling object for convenient chaining.

db_delete()

Deletes the persistent copy of the current model from the database, if has been stored there.

Returns nothing.

in_storage()

Uses DBIx::Class's internal mechanisms to determine if this model is tied to a record in the database.

Returns a true value if it is, otherwise returns a false value.