The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

DBIx::Class::LookupColumn::LookupColumnComponent - A dbic component for building accessors for a lookup table.

SYNOPSIS

 # ===== 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'); 

DESCRIPTION

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.

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.

METHODS

add_lookup

 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.

It will add three methods to the class: see "GENERATED METHODS".

Arguments:

$relation_name

The name of the relation, used for making default names for the generated methods.

$foreign_key

the foreign key column in the table on which to add the lookup relation.

$lookup_table

The Lookup table, on which the foreign key points.

\%options?

An optional HashRef, with the following keys:

name_accessor

the name of the generated accessor, defaults to ${relation_name}

name_setter

the name of the generated setter, defaults to set_${relation_name}

name_checker

the name of the generated method (checker), defaults to 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.

GENERATED METHODS

name_accessor

Return the value/definition/name in the target lookup table, storing the whole looup table in the cache if not already done.

Example:

 print User->find($user_id)->type; # 'Administrator'

name_setter

Set the foreign key in the instance to point to the given value/definition/name.

Example:

 User->find($user_id)->set_type('Guest')

name_checker

Test if the lookup value of the row instance points to the same value as the argument.

Example:

 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'.

AUTHORS

Karl Forner <karl.forner@gmail.com>

Thomas Rubattel <rubattel@cpan.org>

BUGS

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.

SUPPORT

You can find documentation for this module with the perldoc command.

    perldoc DBIx::Class::LookupColumn::LookupColumnComponent

You can also look for information at:

LICENCE AND COPYRIGHT

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.