PrePAN

Sign in to PrePAN

Data::Mapper An implementation of Data Mapper Pattern described in PofEAA

Good

Synopsis

use Data::Mapper;
use Data::Mapper::Adapter::DBI;

my $dbh     = DBI->connect($dsn, $username, $password, ...);
my $adapter = Data::Mapper::Adapter::DBI->new({ driver => $dbh });
my $mapper  = Data::Mapper->new({ adapter => $adapter });

# Create
my $data = $mapper->create(user => { name => 'kentaro', age => 34 });

# Retrieve just one item
$data = $mapper->find(user => { name => 'kentaro' });
$data->param('name'); #=> kentaro
$data->param('age');  #=> kentaro

# Retrieve all with some conditions
$result = $mapper->all(user => { age => 34 }, { order_by => 'id DESC' });

for my $data (@$result) {
    $data->param('name');
    ...
}

# Update
$data->param(age => 35);
my $sth = $mapper->update(user => $data->changes, { name => $data->param('name') });
$sth->rows; #=> 1

# Destroy
my $sth = $mapper->destroy(user => { name => $data->param('name') });
$sth->rows; #=> 1

Description

Data::Mapper is an implementation of Data Mapper Pattern described in PofEAA, written by Martin Fowler, and is kind of a ORM, but not limited only to it, that is, this module just relates some data to another; for example, data from a database to Perl's objects.

http://www.martinfowler.com/eaaCatalog/dataMapper.html

This module, actually, merely defines a simple convention how to make relations between some data to another, and now has only one adapter implementation: Data::Mapper::Adapter::DBI.

See https://github.com/kentaro/data-mapper for details.

Comments

Please sign up to post a review.