The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Gantry::Utils::ModelHelper - mixin for model base classes

SYNOPSIS

    use Gantry::Utils::ModelHelper qw(
        db_Main
        retrieve_all_for_main_listing
        get_form_selections
    );

    sub get_db_options {
        return {};  # put your default options here
        # consider calling __PACKAGE->_default_attributes
    }

DESCRIPTION

This module provides mixin methods commonly needed by model base classes. Note that you must request the methods you want for the mixin scheme to work. Also note that you can request either db_Main or auth_db_Main, but not both. Whichever one you choose will be exported as db_Main in your package.

METHODS

db_Main

This method returns a valid dbh using the scheme described in Gantry::Docs::DBConn. It is compatible with Class::DBI.

auth_db_Main

This method is exported as db_Main and works with the scheme described in Gantry::Docs::DBConn. It too is compatible with Class::DBI.

get_form_selections

This method gives you a selection list for each foriegn key in your table. The lists come to you as a single hash keyed by the table names of the foreign keys. The values in the hash are ready for use by form.tt as options on the field (whose type should be select). Example:

    {
        status => [
            { value => 2, label => 'Billed' },
            { value => 1, label => 'In Progress' },
            { value => 3, label => 'Paid' },
        ],
        other_table => [ ... ],
    }

To use this method, your models must implement these class methods:

get_foreign_tables

(Must be implemented by the model on which get_form_selections is called.) Returns a list of the fully qualified package names of the models of this table's foreign keys. Example:

    sub get_foreign_tables {
        return qw(
            Apps::AppName::Model::users
            Apps::AppName::Model::other_table
        );
    }
get_foreign_display_fields

(Must be implemented by all the models of this table's foreign keys.) Returns an array reference whose elements are the names of the columns which will appear on the screen in the selection list. Example:

    sub get_foreign_display_fields {
        return [ qw( last_name first_name ) ];
    }
retrieve_all_for_main_listing

Returns a list of row objects (one for each row in the table) in order by their foreign_display columns.

AUTHOR

Phil Crow <philcrow2000@yahoo.com>

COPYRIGHT and LICENSE

Copyright (c) 2006, Phil Crow

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.