Baldrick::SpecialTurnip

From CunningPlans

Jump to: navigation, search

[edit] package Baldrick::SpecialTurnip

The class Baldrick::SpecialTurnip is a base class for objects that represent a database record or a JOIN of database records. In the MVC framework, SpecialTurnips are part of the Model. A SpecialTurnip knows how to load itself from the database, receive and validate edits (from a web form perhaps), and save its modified fields back into the database record(s).

Baldrick::SpecialTurnip is never directly instantiated. Application authors should subclass it to create objects that represent entities within the application.

SpecialTurnip instances might be a blog posting, a shopping cart item, a shopping cart order, or any other object that comes from a database row or a multi-table join.

A User object can even be a SpecialTurnip, if your custom user class inherits from both SpecialTurnip and Baldrick::User.

[edit] Usage

 package MyMessageBoardPosting;
 use base 'Baldrick::SpecialTurnip;'
 our %INIT_ARGS = (
     tableinfo => {
         MessageBoardPostings => {
             primarykey => [ 'postid' ],    # required: list of primary key fields in this table.
             pksequence_postid => 'newpostsequence',     # optional - if creating records, where to get new PK values
             fieldmap => {
                 boardid => 'integer',
                 postid => 'integer', 
                 author => 'length(0,20)',   ## a Baldrick::InputValidator rule to validate this field,
                 subject => 'length(0,100)', 
                 ...
             },
         }
     }, 
     metainfo => {
            # loadquery is optional for one-table SpecialTurnips, required for multi-table SpecialTurnips
            loadquery => 'select * from MessageBoardPostings', 
            readonly_TABLENAME => 0, # set to 1 and it'll never touch the table - useful for multi-table turnips.
     }
 );
 
 sub getInitArgs { return \%INIT_ARGS; }  # you must always define getInitArgs()

[edit] Interface

Personal tools