NAME
YAWF::Object - Base class for YAWF ojects
SYNOPSIS
my $object = YAWF::Object->new(); # New item
my $object = YAWF::Object->new( $id ); # Get item by primary key
my $object = YAWF::Object->new( foo => bar ); # Search item (returns the first)
my @objects = YAWF::Object->list({ foo => bar }); # Search for a list of items
DESCRIPTION
YAWF::Object is the base class for database objects. It abstracts DBIx::Class and adds some nice functions which also could be used via Template::Toolkit.
Additional methods could be added to objects as needed.
USAGE EXAMPLE
Here is a sample defining an user object for MyProject:
package MyProject::User;
use strict;
use warnings;
use constant TABLE => 'Users';
use YAWF::Object;
our @ISA = ('YAWF::Object','MyProject::DB::Result::Users');
1;
Extra features could be added on demand:
package MyProject::User;
use strict;
use warnings;
use constant TABLE => 'Users';
use YAWF::Object;
our @ISA = ('YAWF::Object','MyProject::DB::Result::Users');
# A method for getting a list of all the friends of this user, could
# be used from Template::Toolkit, too.
sub friends {
my $self = shift;
return MyProject::Friends->list({ myuserid => $self->userid });
}
# Overriding a column name
sub lastlogin {
my $self = shift;
if ((time - $self->to_time('lastlogin')) < 86400) {
return 'today';
} else {
return 'long ago';
}
}
1;
CLASS METHODS
list
my @objects = YAWF::Object->list(); # Get all items of a table (could be big!)
my @objects = YAWF::Object->list({ foo => bar }); # Search for a list of items
count
my $count = YAWF::Object->count(); # Get the number of items in this table
my $count = YAWF::Object->count({ foo => bar }); # Get the number of items for this search
METHODS
new
my $object = YAWF::Object->new(); # New item
my $object = YAWF::Object->new($id); # Get item by primary key
my $object = YAWF::Object->new(foo => bar); # Search item (returns the first)
The new
constructor lets you create a new YAWF::Object object.
The first syntax creates a new, empty item while the others return an existing item from the database or undef if nothing was found.
flush
$object->flush;
Write a YAWF object into the database with automatic selection of insert or update depending on the objects state (new or existing).
Changes the variable used to call the method to the new object and also returns the new object.
to_time
my $timestamp = $object->to_time($time_column);
Convertes an SQL ISO timestamp to an unixtime value.
from_time
my $timestamp = $object->from_time($time_column,$timestamp);
Inserts a timestamp into the database (converting it to SQL format).
SUPPORT
No support is available
AUTHOR
Copyright 2011 Sebastian Willing.