PUBLIC

Bedrock::Model - Yet another ORM for MySQL

SYNOPSIS

my $model = Bedrock::Model->new(
  { dbi   => $dbi,
    table => $table,
    model => $model_specs
  }
);

$model->create_table();

# see if we need to update schema
my $migration = $model->migrate_model(dry_run => 1);

...or as an ORM...

package MyApp::Users;

use parent qw(Bedrock::Model::Handler);

our $MODEL = {

    id => Bedrock::Model::Field->new(
        {
            field => 'id',
            type  => 'int(11)',
            null  => 'no',
            extra => 'auto_increment',
            key   => 'pri'
        }
    ),
    username => Bedrock::Model::Field->new(
        {
            field => 'username,
            type  => 'varchar(100)',
            null  => 'no'
        }
    ),
    email => Bedrock::Model::Field->new(
        {
            field => 'email',
            type  => 'varchar(100)',
            null  => 'yes'
        }
    ),
    %Bedrock::Model::Timestampable,
};

1;

...then...

MyApp::Users->create($dbi);

my $users = MyApp::Users->($dbi);
$user->set(email => 'someuser@example.com');
$user->save();

DESCRIPTION

Class for creating/modifying MySQL tables.

METHODS AND SUBROUTINES

new

new({ dbi => dbi, table => table-name, model => model});

add_field

add_field(
  field => name,
  type  => type,
  [ extra => extra-options, default => default, key => ]
);

create_table

create_table()
create_table( dbi => dbi, table => table-name, model => model );

See for argument descriptions.

Creates a new table based on the object's model.

create

create(dbi, [ model => model, table => table ] );

Foo::Bar->create($dbi, [ model => model, table => table, no_warnings => 1 ]);

Method used to create a new table and return a Bedrock::Model instance. This method can be called as an instance or class method. If the table exists a check will be made against the model to see if a migration is required. You can call get_migration() to return the Bedrock::Model::Migration object.

Throws an exception if the table cannot be created.

Throws an exception of no valid model is defined for the class.

Returns a Bedrock::Model object.

describe_table

describe_table(dbi => dbi-handle, table => table-name, serializer => serializer)

Returns the model definition from the table or the create statement needed to build the table.

Pass no arguments if you want the create statement returned or pass at least the dbi-handle if you want the model definition.

fetch_model

Returns a reference to a hash that represents the model derived directly from the MySQL table. The hash contains the columns as keys and Bedrock::Model::Field objects as values.

migrate_model

Creates a Bedrock::Model::Migration object suitable for querying to determine if a migration is required and suitable for execution.

my $migration = $model->migrate_model();

if ( $migration->should_migrate ) {
  $migration->execute();
}

rename_field

rename_field(old-field, new-field)

Renames a column. Note that this method will alter the database and the model.

verify_model

Creates a Bedrock::Model::Migration object suitable for querying to determine if a migration is required, but not suitable for execution.

my $migration = $model->verify_model();

if ( $migration->should_migrate ) {
  print STDERR "model our of sync with database table...";
}

AUTHOR

Rob Lauer - rlauer6@comcast.net

SEE ALSO

Bedrock::Model::Field, BLM::IndexedTableHandler, Bedrock::Model::Handler