YAWF::Object - Base class for YAWF ojects
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
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.
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;
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
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
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.
new
The first syntax creates a new, empty item while the others return an existing item from the database or undef if nothing was found.
$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.
my $timestamp = $object->to_time($time_column);
Convertes an SQL ISO timestamp to an unixtime value.
my $timestamp = $object->from_time($time_column,$timestamp);
Inserts a timestamp into the database (converting it to SQL format).
No support is available
Copyright 2011 Sebastian Willing.
To install YAWF, copy and paste the appropriate command in to your terminal.
cpanm
cpanm YAWF
CPAN shell
perl -MCPAN -e shell install YAWF
For more information on module installation, please visit the detailed CPAN module installation guide.