ActiveRecord-Simple
view release on metacpan or search on metacpan
lib/ActiveRecord/Simple.pm view on Meta::CPAN
root_class => $class,
m_class => _get_related_subclass($relation),
via_table => $relation->{via_table},
self => $self,
});
}
return $self->{$instance_name};
}
}
elsif ($full_relation_type eq 'generic_to_generic') {
*{$pkg_method_name} = sub {
my ($self, @args) = @_;
if (!$self->{$instance_name}) {
my %find_attrs;
while (my ($k, $v) = each %{ $relation->{key} }) {
$find_attrs{$v} = $self->{$k};
}
$self->{$instance_name} = $related_class->find(\%find_attrs);
}
return $self->{$instance_name};
}
}
}
use strict 'refs';
}
sub _what_is_the_table_name {
my $class = ref $_[0] ? ref $_[0] : $_[0];
croak 'Invalid data class' if $class =~ /^ActiveRecord::Simple/;
my $table_name =
$class->can('_get_table_name') ?
$class->_get_table_name
: class_to_table_name($class);
return $table_name;
}
1;
__END__;
=head1 NAME
ActiveRecord::Simple - Simple to use lightweight implementation of ActiveRecord pattern.
=head1 DESCRIPTION
ActiveRecord::Simple is a simple lightweight implementation of ActiveRecord
pattern. It's fast, very simple and very light.
=head1 SYNOPSIS
package Model;
use parent 'ActiveRecord::Simple';
# connect to the database:
__PACKAGE__->connect($dsn, $opts);
package Customer;
use parent 'Model';
__PACKAGE__->table_name('customer');
__PACKAGE__->columns(qw/id first_name last_login/);
__PACKAGE__->primary_key('id');
__PACKAGE__->has_many(purchases => 'Purchase');
package Purchase;
use parent 'Model';
__PACKAGE__->auto_load(); ### load table_name, columns and primary key from the database automatically
__PACKAGE__->belongs_to(customer => 'Customer');
package main;
# get customer with id = 1:
my $customer = Customer->objects->find({ id => 1 })->fetch();
# or (the same):
my $customer = Customer->objects->get(1);
print $customer->first_name; # print first name
$customer->last_login(\'NOW()'); # to use built-in database function just send it as a SCALAR ref
$customer->save(); # save in the database
# get all purchases of $customer:
my @purchases = Purchase->objects->find(customer => $customer)->fetch();
# or (the same):
my @purchases = $customer->purchases->fetch();
# order, group and limit:
my @purchases = $customer->purchases->order_by('paid')->desc->group_by('kind')->limit(10)->fetch();
=head1 CLASS METHODS
L<ActiveRecord::Simple> implements the following class methods.
=head2 new
Object's constructor.
my $log = Log->new(message => 'hello', level => 'info');
=head2 connect
Connect to the database, uses DBIx::Connector if installed, if it's not - L<ActiveRecord::Simple::Connect>.
__PACKAGE__->connect($dsn, $username, $password, $options);
=head2 dbh
Access to the database handler. Undef if it's not connected.
__PACKAGE__->dbh->do('SELECT 1');
=head2 table_name
Set table name.
__PACKAGE__->table_name('log');
=head2 columns
( run in 0.672 second using v1.01-cache-2.11-cpan-2398b32b56e )