DBIx::Class::LookupColumn::LookupColumnComponent - A dbic component for building accessors for a lookup table.
# ===== use the component in your table definition ===== package MySchema::Result::User; __PACKAGE__->load_components( B<qw/LookupColumn/> );> __PACKAGE__->table("user"); __PACKAGE__->add_columns( "user_id",{}, "name", {}, "user_type_id", {} ); __PACKAGE__->set_primary_key("user_id"); __PACKAGE__->add_lookup( 'type', 'user_type_id', 'UserType' ); # === use the generated accessors === $user->type; # fetches the type value (e.g. 'Administrator') directly from the cache # checks that 'Administrator' is a valid value, get its id, and tests if it matches $user->user_type_id $user->is_type('Administrator'); # checks that 'User' is a valid value, get its id, and sets it as $user->user_type_id $user->set_type('User');
This is the actual implementation of DBIx::Class::LookupColumn, that is why you can and should use 'LookupColumn' instead of 'LookupColumn::LookupColumnComponent' in the load_components function call.
'LookupColumn'
'LookupColumn::LookupColumnComponent'
load_components
This module generates convenient methods (accessors) for accessing data in a Lookup table (see "Lookup Tables" in DBIx::Class::LookupColumn. It uses DBIx::Class::LookupColumn::Manager to cache and store the entire lookup tables in memory.
add_lookup( $relation_name, $foreign_key, $lookup_table, \%options?)
Add a Lookup relation from a Table to a Lookup table from a foreign key by generating new accessors and setters. The relation is defined by its name ( $relation_name ), the foreign key and the Lookup table.
$relation_name
It will add three methods to the class: see "GENERATED METHODS".
Arguments:
The name of the relation, used for making default names for the generated methods.
the foreign key column in the table on which to add the lookup relation.
The Lookup table, on which the foreign key points.
An optional HashRef, with the following keys:
the name of the generated accessor, defaults to ${relation_name}
${relation_name}
the name of the generated setter, defaults to set_${relation_name}
set_${relation_name}
the name of the generated method (checker), defaults to is_${relation_name}
is_${relation_name}
Example:
MySchema::Result::User->add_lookup( 'permission', 'permission_type_id', 'PermissionType', {name_accessor => 'get_the_permission', name_setter => 'set_the_permission, name_checker => 'is_the_permission' } );
Will add methods get_the_permission, set_the_permission and is_the_permission in MySchema::Result::User.
get_the_permission
set_the_permission
is_the_permission
Return the value/definition/name in the target lookup table, storing the whole looup table in the cache if not already done.
print User->find($user_id)->type; # 'Administrator'
Set the foreign key in the instance to point to the given value/definition/name.
User->find($user_id)->set_type('Guest')
Test if the lookup value of the row instance points to the same value as the argument.
User->find($user_id)->is_type('Guest')
Returns true if the value in the Lookup Table UserType associated with the key User->find($user_id)->user_type_id is equals to 'Guest'.
Karl Forner <karl.forner@gmail.com>
Thomas Rubattel <rubattel@cpan.org>
Please report any bugs or feature requests to bug-dbix-class-lookupcolumn-lookupcolumncomponent at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-Class-LookupColumn-LookupColumnComponent. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-dbix-class-lookupcolumn-lookupcolumncomponent at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc DBIx::Class::LookupColumn::LookupColumnComponent
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-Class-LookupColumn-LookupColumnComponent
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/DBIx-Class-LookupColumn-LookupColumnComponent
CPAN Ratings
http://cpanratings.perl.org/d/DBIx-Class-LookupColumn-LookupColumnComponent
Search CPAN
http://search.cpan.org/dist/DBIx-Class-LookupColumn-LookupColumnComponent/
Copyright 2012 Karl Forner and Thomas Rubattel, All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the terms as Perl itself.
To install DBIx::Class::LookupColumn, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::Class::LookupColumn
CPAN shell
perl -MCPAN -e shell install DBIx::Class::LookupColumn
For more information on module installation, please visit the detailed CPAN module installation guide.