Alexander Ponomarev > ActiveRecord-Simple > ActiveRecord::Simple



Annotate this POD


New  1
Open  0
View/Report Bugs
Module Version: 0.65   Source  


ActiveRecord::Simple - Simple to use lightweight implementation of ActiveRecord pattern.


Version 0.65






ActiveRecord::Simple is a simple lightweight implementation of ActiveRecord pattern. It's fast, very simple and very light.


    package MyModel:Person;

    use base 'ActiveRecord::Simple';

    __PACKAGE__->columns('id', 'name');


That's it! Now you're ready to use your active-record class in the application:

    use MyModel::Person;

    # to create a new record:
    my $person = MyModel::Person->new({ name => 'Foo' })->save();

    # to update the record:

    # to get the record (using primary key):
    my $person = MyModel::Person->get(1);

    # to get the record with specified fields:
    my $person = MyModel::Person->find(1)->only('name', 'age')->fetch;

    # to find records by parameters:
    my @persons = MyModel::Person->find({ name => 'Foo' })->fetch();

    # to find records by sql-condition:
    my @persons = MyModel::Person->find('name = ?', 'Foo')->fetch();

    # also you can do something like this:
    my $persons = MyModel::Person->find('name = ?', 'Foo');
    while ( my $person = $persons->fetch() ) {
        say $person->name;

    # You can add any relationships to your tables:
    __PACKAGE__->has_many(cars => 'MyModel::Car' => 'id_preson');
    __PACKAGE__->belongs_to(wife => 'MyModel::Wife' => 'id_person');

    # And then, you're ready to go:
    say $person->cars->fetch->id; # if the relation is one to many
    say $person->wife->name; # if the relation is one to one
    $person->wife(Wife->new({ name => 'Jane', age => '18' })->save)->save; # change wife ;-)


ActiveRecord::Simple provides a variety of techniques to make your work with data little easier. It contains only a basic set of operations, such as search, create, update and delete data.

If you realy need more complicated solution, just try to expand on it with your own methods.

Class Methods ^

Class methods mean that you can't do something with a separate row of the table, but they need to manipulate of the table as a whole object. You may find a row in the table or keep database handler etc.


Creates a new object, one row of the data.

    MyModel::Person->new({ name => 'Foo', second_name => 'Bar' });

It's a constructor of your class and it doesn't save a data in the database, just creates a new record in memory.


    __PACKAGE__->columns([qw/id_person first_name second_name]);
    # or
    __PACKAGE__->columns('id_person', 'first_name', 'second_name');
    # or
        id_person => {
            # ...
        first_name => {
            # ...
        second_name => {
            # ...
    # or
        id_person => {
            # ...
        first_name => {
            # ...
        second_name => {
            # ...

This method is required. Set names of the table columns and add accessors to object of the class. If you set a hash or a hashref with additional parameters, the method will be dispatched to another method, "fields".


        id_person => {
            data_type => 'int',
            is_auto_increment => 1,
            is_primary_key => 1
        first_name => {
            data_type => 'varchar',
            size => 64,
            is_nullable => 0
        second_name => {
            data_type => 'varchar',
            size => 64,
            is_nullable => 0,

This method requires SQL::Translator to be installed. Create SQL-Schema and data type validation for each specified field using SQL::Translator features. You don't need to call "columns" method explicitly, if you use "fields".

See SQL::Translator for more information about schema and SQL::Translator::Field for information about available data types.



Set name of the primary key. This method is not required to use in the child (your model) classes.



If you don't need to use primary key, but need to insert or update data, using specific parameters, you can try this one: secondary key. It doesn't reflect schema, it's just about the code.


    __PACKAGE__->index('index_id_person', ['id_person']);

Create an index and add it to the schema. Works only when method "fields" is using.



Set name of the table. This method is required to use in the child (your model) classes.

relations [!OLD!, may be deprecated in the future]

        cars => {
            class => 'MyModel::Car',
            key   => 'id_person',
            type  => 'many'

It's not a required method and you don't have to use it if you don't want to use any relationships in your tables and objects. However, if you need to, just keep this simple schema in youre mind:

        [relation key] => {
            class => [class name],
            key   => [column that refferers to the table],
            type  => [many or one]

    [relation key] - this is a key that will be provide the access to instance
    of the another class (which is specified in the option "class" below),
    associated with this relationship. Allowed to use as many keys as you need:

    $package_instance->[relation key]->[any method from the related class];


    __PACKAGE__->belongs_to(home => 'Home');

This method describes one-to-one objects relationship. By default ARS think that primary key name is "id", foreign key name is "[table_name]_id". You can specify it by parameters:

    __PACKAGE__->belongs_to(home => 'Home', {
        primary_key => 'id',
        foreign_key => 'home_id'


    __PACKAGE__->has_many(cars => 'Car');
    __PACKAGE__->has_many(cars => 'Car', {
        primary_key => 'id',
        foreign_key => 'car_id'

This method describes one-to-many objects relationship.


    __PACKAGE__->has_one(wife => 'Wife');
    __PACKAGE__->has_one(wife => 'Wife', {
        primary_key => 'id',
        foreign_key => 'wife_id'

You can specify one object via another one using "has_one" method. It works like that:

    say $person->wife->name; # SELECT name FROM Wife WHERE person_id = $self._primary_key


    __PACKAGE__->generic(photos => { release_date => 'pub_date' });

    Creates a generic relations.

    my $single = Song->find({ type => 'single' })->fetch();
    my @photos = $single->photos->fetch();  # fetch all photos with pub_date = single.release_date


This method provides two features:

   1. Check the changes of object's data before saving in the database.
      Won't save if data didn't change.

   2. Automatic save on object destroy (You don't need use "save()" method



There are several ways to find someone in your database using ActiveRecord::Simple:

    # by "nothing"
    # just leave attributes blank to recieve all rows from the database:
    my @all_persons = MyModel::Person->find()->fetch;

    # by primary key:
    my $person = MyModel::Person->find(1)->fetch;

    # by multiple primary keys
    my @persons = MyModel::Person->find([1, 2, 5])->fetch;

    # by simple condition:
    my @persons = MyModel::Person->find({ name => 'Foo' })->fetch;

    # by where-condtions:
    my @persons = MyModel::Person->find('first_name = ? and id_person > ?', 'Foo', 1);

If you want to get a few instances by primary keys, you should put it as arrayref, and then fetch from resultset:

    my @persons = MyModel::Person->find([1, 2])->fetch();

    # you don't have to fetch it immidiatly, of course:
    my $resultset = MyModel::Person->find([1, 2]);
    while ( my $person = $resultset->fetch() ) {
        say $person->first_name;

To find some rows by simple condition, use a hashref:

    my @persons = MyModel::Person->find({ first_name => 'Foo' })->fetch();

Simple condition means that you can use only this type of it:

    { first_name => 'Foo' } goes to "first_type = 'Foo'";
    { first_name => 'Foo', id_person => 1 } goes to "first_type = 'Foo' and id_person = 1";

If you want to use a real sql where-condition:

    my $res = MyModel::Person->find('first_name = ? or id_person > ?', 'Foo', 1);
    # select * from persons where first_name = "Foo" or id_person > 1;

You can use the ordering of results, such as ORDER BY, ASC and DESC:

    my @persons = MyModel::Person->find('age > ?', 21)->order_by('name')->desc->fetch();
    my @persons = MyModel::Person->find('age > ?', 21)->order_by('name', 'age')->fetch();


Returns count of records that match the rule:

    say MyModel::Person->count;
    say MyModel::Person->count({ zip => '12345' });
    say MyModel::Person->count('age > ?', 55);


Returns 1 if record is exists in database:

    say "Exists" if MyModel::Person->exists({ zip => '12345' });
    say "Exists" if MyModel::Person->exists('age > ?', 55);


Returns the first record (records) ordered by the primary key:

    my $first_person = MyModel::Person->first->fetch;
    my @ten_persons  = MyModel::Person->first(10)->fetch;


Returns the last record (records) ordered by the primary key:

    my $last_person = MyModel::Person->last->fetch;
    my @ten_persons = MyModel::Person->last(10)->fetch;


Increment the field value:

    my $person = MyModel::Person->get(1);
    say $person->age;  # prints e.g. 99
    say $person->age; # prints 100


Decrement the field value:

    my $person = MyModel::Person->get(1);
    say $person->age;  # prints e.g. 100
    say $person->age; # prints 99


    say MyModel::Person->as_sql('PostgreSQL');

This method requires SQL::Translator to be installed. Create an SQL-schema using method "fields". See SQL::Translator for more details.


Keeps a database connection handler. It's not a class method actually, this is an attribute of the base class and you can put your database handler in any class:


Or even rigth in base class:


This decision is up to you. Anyway, this is a singleton value, and keeps only once at the session.

Object Methods ^

Object methods usefull to manipulating single rows as a separate objects.


Get only those fields that are needed:

    my $person = MyModel::Person->find({ name => 'Alex' })->only('address', 'email')->fetch;
    ### SQL:
    ###     SELECT `address`, `email` from `persons` where `name` = "Alex";


This is shortcut method for "find":

    my $person = MyModel::Person->get(1);
    ### is the same:
    my $person = MyModel::Person->find(1)->fetch;


Order your results by specified fields:

    my @persons = MyModel::Person->find({ city => 'NY' })->order_by('name')->fetch();

This method uses as many fields as you want:

    my @fields = ('name', 'age', 'zip');
    my @persons = MyModel::Person->find({ city => 'NY' })->order_by(@fields)->fetch();


Use this attribute to order your results ascending:

    MyModel::Person->find([1, 3, 5, 2])->order_by('id')->asc->fetch();


Use this attribute to order your results descending:

    MyModel::Person->find([1, 3, 5, 2])->order_by('id')->desc->fetch();


Use this attribute to limit results of your requests:

    MyModel::Person->find()->limit(10)->fetch; # select only 10 rows


Offset of results:

    MyModel::Person->find()->offset(10)->fetch; # all next after 10 rows


To insert or update data in the table, use only one method. It detects automatically what do you want to do with it. If your object was created by the new method and never has been saved before, method will insert your data.

If you took the object using the find method, "save" will mean "update".

    my $person = MyModel::Person->new({
        first_name => 'Foo',
        secnd_name => 'Bar',

    $person->save() # -> insert

    $person->save() # -> now it's update!

    ### or

    my $person = MyModel::Person->find(1);
    $person->save() # update



Delete row from the table.


Checks for a record in the database corresponding to the object:

    my $person = MyModel::Person->new({
        first_name => 'Foo',
        secnd_name => 'Bar',

    $person->save() unless $person->exists;


Convert objects data to the simple perl hash:

    use JSON::XS;

    say encode_json({ person => $peron->to_hash });


Convert aobject to SQL-query:

    my $sql = Person->find({ name => 'Bill' })->limit(1)->to_sql;
    # select * from persons where name = ? limit 1;

    my ($sql, $binds) = Person->find({ name => 'Bill' })->to_sql;
    # sql: select * from persons where name = ? limit 1;
    # binds: ['Bill']


Checks weather an object is defined:

    my $person = MyModel::Person->find(1);
    return unless $person->is_defined;


When you use the "find" method to get a few rows from the table, you get the meta-object with a several objects inside. To use all of them or only a part, use the "fetch" method:

    my @persons = MyModel::Person->find('id_person != ?', 1)->fetch();

You can also specify how many objects you want to use at a time:

    my @persons = MyModel::Person->find('id_person != ?', 1)->fetch(2);
    # fetching only 2 objects.

Another syntax of command "fetch" allows you to make read-only objects:

    my @persons = MyModel::Person->find->fetch({ read_only => 1, limit => 2 });
    # all two object are read-only


   use ACTIVE_RECORD_SIMPLE_TRACE=1 environment variable:



    L<DBIx::ActiveRecord>, L<SQL::Translator>


    perldoc ActiveRecord::Simple::Tutorial


shootnix, <shootnix at>


Please report any bugs or feature requests to, or through the github:


You can find documentation for this module with the perldoc command.

    perldoc ActiveRecord::Simple

You can also look for information at:



Copyright 2013 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 for more information.

syntax highlighting: