ActiveRecord::Simple - Simple to use lightweight implementation of ActiveRecord pattern.
ActiveRecord::Simple is a simple lightweight implementation of ActiveRecord pattern. It's fast, very simple and very light.
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();
ActiveRecord::Simple implements the following class methods.
Object's constructor.
my $log = Log->new(message => 'hello', level => 'info');
Connect to the database, uses DBIx::Connector if installed, if it's not - ActiveRecord::Simple::Connect.
__PACKAGE__->connect($dsn, $username, $password, $options);
Access to the database handler. Undef if it's not connected.
__PACKAGE__->dbh->do('SELECT 1');
Set table name.
__PACKAGE__->table_name('log');
Set columns. Make accessors if make_columns_accessors not 0 (default is 1)
__PACKAGE__->columns('id', 'time');
Set primary key. Optional parameter.
__PACKAGE__->primary_key('id');
Set secondary key.
__PACKAGE__->secondary_key('time');
Load table_name, columns and primary_key from table_info (automatically from database).
__PACKAGE__->auto_load();
Create a ralation to another table (many-to-many, many-to-one).
Customer->has_many(purchases => 'Purchase'); # if you need to set a many-to-many relation, you have to # specify a third table using "via" key: Pizza->has_many(toppings => 'Topping', { via => 'pizza_topping' });
Create a relation to another table (one-to-many, one-to-one). Foreign key is an optional parameter, default is <table tane>_id.
Purchase->belongs_to(customer => 'Customer'); # or Purchase->belong_to(customer => 'Customer', { fk => 'customer_id' });
Create a relation to another table (one-to-one).
Customer->has_one(address => 'Address');
Create a relation without foreign keys:
Meal->generic(critical_t => 'Weather', { t_max => 't' });
Set to 0 before method 'columns' if you don't want to make accessors to columns:
__PACKAGE__->make_columns_accessors(0); __PACKAGE__->columns('id', 'time'); # now you can't get $log->id and $log->time, only $log->{id} and $log->{time};
Create calculated fields
Purchase->mixins( sum_amount => sub { return 'SUM(amount)' } ); # and then my $purchase = Purchase->find({ id => 1 })->fields('id', 'title', 'amount', 'sum_amount')->fetch;
Make a relation. The method is aoutdated.
Returns instance of ActiveRecord::Simple::QueryManager.
Returns ActiveRecord::Simple::Find object.
my $finder = Customer->find(); # it's like ActiveRecord::Simple::Find->new(); $finder->order_by('id'); my @customers = $finder->fetch;
Same as __PACKAGE__->find->fetch;
Get object by primary_key
my $customer = Customer->get(1); # same as Customer->find({ id => 1 })->fetch;
Get number of rows
my $cnt = Customer->count('age > ?', 21);
Check if row is exists in the database
warn "Got Barak!" if Customer->exists({ name => 'Barak Obama' })
ActiveRecord::Simple implements the following object methods.
Check object is defined
Save object to the database
Delete object from the database
Update object using hashref
$user->update({ last_login => \'NOW()' });
Unbless object, get naked hash
Increment fields
$customer->increment('age')->save;
Decrement fields
$customer->decrement('age')->save;
shootnix, <shootnix at cpan.org>
<shootnix at cpan.org>
Please report any bugs or feature requests to shootnix@cpan.org, or through the github: https://github.com/shootnix/activerecord-simple/issues
shootnix@cpan.org
You can find documentation for this module with the perldoc command.
perldoc ActiveRecord::Simple
You can also look for information at:
Github wiki:
https://github.com/shootnix/activerecord-simple/wiki
Copyright 2013-2018 shootnix.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install ActiveRecord::Simple, copy and paste the appropriate command in to your terminal.
cpanm
cpanm ActiveRecord::Simple
CPAN shell
perl -MCPAN -e shell install ActiveRecord::Simple
For more information on module installation, please visit the detailed CPAN module installation guide.