NAME
DBIx::Simple::OO
SYNOPSIS
use DBIx::Simple;
use DBIx::Simple::OO; # adds OO methods
$db = DBIx::Simple->connect( ... );
$query = 'select id,name,age from people';
$res = $db->query( $query );
$obj = $res->object;
@obj = $res->objects;
$id = $obj->id; # get the value for field 'id'
$name = $obj->name; # get the value for field 'name'
$age = $obj->age; # get the value for field 'age'
@acc = $obj->ls_accessors; # get a list of all fields
$sub = $obj->can('name'); # check if this object has a
# 'name' method
### add a method to every object that will be returned
### by DBIx::Simple::OO
{ package DBIx::Simple::OO::Item;
sub has_valid_id { return shift->id !~ /\D/ ? 1 : 0 }
}
$bool = $obj->has_valid_id;
DESCRIPTION
DBIx::Simple now has support for object
and objects
built into it. Using DBIx::Simple::OO is no longer necessary if you change DBIx::Simple::OO::Item
to DBIx::Simple::Result::RowObject
and use 1.33 or newer. DBIx::Simple::OO 0.02 implements a work-around to avoid name clashes, but using 0.01 with DBIx::Simple 1.33+ could go wrong.
This module provides a possibility to retrieve rows from a database as objects, rather than the traditional array ref
or hash ref
. This provides all the usual benefits of using objects over plain references for accessing data, as well as allowing you to add methods of your own choosing for data retrieval.
HOW IT WORKS
DBIx::Simple::OO
declares its 2 methods in the DBIx::Simple::Result
namespace, transforming the rows retrieved from the database to full fledged objects.
METHODS
This module subclasses DBIx::Simple
and only adds the following methods. Any other method, like the new
call should be looked up in the DBIx::Simple
manpage instead.
$obj = $db->query(....)->object( );
Returns the first result from your query as an object.
@objs = $db->query(....)->objects( );
Returns the results from your query as a list of objects.
ACCESSORS
All objects returned by the above methods are from the DBIx::Simple::OO::Item
class, which subclasses Object::Accessor
.
The most important methods are described in the synopsis, but you should refer to the Object::Accessor
manpage for more extensive documentation.
Note that it is possible to declare methods into the DBIx::Simple::OO::Item
class to extend the functionality of the objects returned by DBIx::Simple::OO
, as also described in the SYNOPSIS
AUTHOR
This module by Jos Boumans <kane@cpan.org>.
COPYRIGHT
This module is copyright (c) 2005 Jos Boumans <kane@cpan.org>. All rights reserved.
This library is free software; you may redistribute and/or modify it under the same terms as Perl itself.