[Dbix-class] FW: DBIx::Class::LookupColumn

uro shima oo_sigma at hotmail.com
Thu Oct 18 08:35:51 GMT 2012


Dear DBIx-Class mailing list



Hereby we'd like to inform you about a new package related
to DBIx::Class.



Its objective is to bring efficient and convenient methods
for dealing with Lookup Tables. We call Lookup tables tables
that are actually catalogs of terms, e.g. the table UserType with such rows=
: ( 1 =3D> 'Administrator' , 2 =3D> 'User' , 3 =3D>
'Guest' ).



Let's have an example of use.



package MySchema::Result::User;

__PACKAGE__->table("user");

__PACKAGE__->add_columns( "user_id",{}, "name", {},
"user_type_id", {} );

__PACKAGE__->belongs_to( "UserType" =3D>
"Schema3::Result::UserType", {"user_type_id" =3D>
"self.user_type_id"} );



# UserType Lookup table, with 2 columns (user_type_id, name)
with rows: ( 1 =3D> 'Administrator' , 2 =3D> 'User' , 3 =3D>
'Guest' )



# $user is a DBIx::Class::Row instance, e.g.
$user=3D$schema->resultset('User')->find( name =3D> 'Flash
Gordon' )

print $user->type; # print 'Administrator', not very
impressive, could be written as $user->user_type()->name()
print $user->type; # same thing, but we are sure that no
database request is done thanks to the cache system



print $user->is_type('Administrator')  ? 'Ok' : 'Access
Restricted';
# equivalent (but more efficient) to =

my $type =3D
$schema->resultset('UserType')->find( name =3D>
'Administrator')
or die "Bad name 'Administrator' for Lookup table UserType";

print $user->user_type_id eq $type->id  ? 'Ok' : 'Access
Restricted';



The features it provides are the following :

- syntactic sugar to accessors, for example by allowing to deal with hard-c=
oded value directly on the client table,
e.g. $user->is_type('Administrator').

- efficiency by means of a cache system for avoiding many repeated identica=
l individual DBMS requests related to one
entire Lookup table.

- accessors generation automation to a whole schema.


Further informations are of course available on the CPAN at
the following URL :
http://search.cpan.org/~rubattel/DBIx-Class-LookupColumn-0.01/lib/DBIx/Clas=
s/LookupColumn.pm


Questions or comments about it are very welcome.

Karl and Thomas
 		 	   		   		 	   		  =

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.scsys.co.uk/pipermail/dbix-class/attachments/20121018/ef6=
f3f0f/attachment.htm


More information about the DBIx-Class mailing list