Tim Keefer > Gantry > Gantry::Utils::ModelHelper

Download:
Gantry-3.64.tar.gz

Dependencies

Annotate this POD

CPAN RT

New  9
Open  3
View/Report Bugs
Source  

NAME ^

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

SYNOPSIS ^

    use Gantry::Utils::ModelHelper qw(
        db_Main
        get_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 and Gantry::Plugins::DBIxClassConn (the later is a mixin which allows easy access to a DBIx::Schema object for controllers).

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 and Gantry::Plugins::DBIxClassConn.

I will repeat, if you ask for this method in your use statement:

    use lib/Gantry/Utils/ModelHelper qw( auth_db_Main ... );

it will come into your namespace as db_Main.

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 ) ];
    }
get_listing

Replacement for retrieve_all_for_main_listing.

Returns a list of row objects (one for each row in the table). The ORDER BY clause is either the same as the foreign_display columns or chosen by you. If you want to supply the order do it like this:

    my @rows = $MODEL->get_listing ( { order_by => 'last, first' } );

Note that your order_by will be used AS IS, so it must be a valid SQL ORDER BY clause, but feel free to include DESC or anything else you and SQL like.

retrieve_all_for_main_listing

DEPRECATED use get_listing instead

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.

syntax highlighting: