Bedrock::Model - Yet another ORM for MySQL
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();
Class for creating/modifying MySQL tables.
new({ dbi => dbi, table => table-name, model => model});
dbi
Reference to a DBI object for an open database connection.
table-name
Name of the table.
model
A hash of key/value pairs where the keys are the column names and the fields are Bedrock::Model::Field objects.
add_field(
field => name,
type => type,
[ extra => extra-options, default => default, key => ]
);
create_table()
create_table( dbi => dbi, table => table-name, model => model );
See
Creates a new table based on the object's model.
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.
model (optional)
A hash of Bedrock::Model::Field definitions where the keys are the
names of each field or an array of Bedrock::Model::Field
definitons.
table (optional)
The name of the table to create. If you don't pass the table name, the
table name will be derived from the suffix of the class name if
the class subclasses Bedrock::Model.
no_warnings
If true will suppress warning messages when the table exists or needs migration.
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.
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.
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(old-field, new-field)
Renames a column. Note that this method will alter the database and the 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...";
}
Rob Lauer - rlauer6@comcast.net
Bedrock::Model::Field, BLM::IndexedTableHandler, Bedrock::Model::Handler