YaBB3::BaseHash - A database record backed object.


Name

YaBB3::BaseHash - A database record backed object.


Synopsis

  package SubClass;
  use base qw(YaBB3::BaseHash);
  # Access an existing record.
  my $row = $someDatabaseGetRecordMethod(...);      # Get the fields of a database record.
  my $existing = new SubClass($row);                # Create with the record fields.
  my $field    = $existing->{values}->{FieldName};  # Access a record field.
  # Create a new record.
  my $empty    = new SubClass;                      # An empty and uncommitted object.
  $empty->{newvalues} = {                           # New values to commit.
      Field => "value",
      ...
  };
  $empty->subclassCommitMethod();                   # Use a sub-class method to commit the
                                                    # newvalues to a database record.
  $empty->mergeValues();                            # Merge newvalues into committed values.
  $empty->{newvalues} = {};                         # Clear the newvalues.


Overview

An object that represents a record in a database. The object can be used to query or update the current state of the record.

This class is intended only as a super-class (abstract), and should not be directly instantiated.


Description

The record fields are accessed from two object fields.

  1. The values field is a hash of the fields that have been committed to the record.

  2. The newvalues field is a hash of the fields that have not yet been committed to the record.

Creating the object

The Base object is intended to only be used as a super-class of classes that access database records. It should never be created itself. Base provides three ways to create the sub-class:

  1. An empty and uncommitted object
  2. Use the new() method with no arguments to create an uncommitted object. Normally you would add new field values to commit to the backing record.

  3. A committed object
  4. Use the new($row) with a hash of the fields from the backing record to create a committed object through which you can access the current record field values.

  5. A clone.
  6. Use the clone() method to create a copy of an existing object. Sub-classes will usually need to override this method to first call this SUPER version, and then to generate the value of any fields that must be unique (like a record index or id).

Accessing the record

Either create the object with an argument that is a reference to a hash containing the record fields perviously read, or create an empty object and assign the values field with the queried record fields.

Updating the record

Create an empty, or use an existing, object. Fill the newvalues hash with the updated field values. Use a sub-class method to update the record with the newvalues values. Call the mergeValues() method to merge the new values into the committed values. Clear the newvalues hash.


Reference

Use Packages

  Exporter
  YaBB3           $Y

Exports

  Default   $VERSION $Y
  Optional
  Tags

Package Fields

  $VERSION  The package version number as a float.

Package Subroutines

Object Fields

values
The backing record fields that have been committed.

newvalues
The backing record fields that will be committed.

Object Methods

new

  $%       [Opt] Committed record fields.
  Returns
  \%       A Base object reference.

Create and return a new Base object. Optionally, the current state of the object can be synchronized with the current state of the backing record by supplying a hash of the current record fields that will be loaded into the values object field hash.

clone $deep Cloning by creating a deep copy? Standard is false = shallow copy. Returns \% A Base object reference.

Create and return a new Base object that has the same field values as this Base object. Sub-classes will want to override this to generate any fields that should have unique values.

mergeValues

  Returns
  $        1

Merges the entries in the newvalues hash to the values hash. This should only be done after the newvalues entries have been committed to the backing record.

Class Methods

deepClone every type the datastructure to clone Returns parameter type the clone of the passed argument

Deep clones the passed datastructure and returns the copy. Thanks to Garrett Goebel (garrett@scriptpro.com) who coded this! See also http://dev.perl.org/perl6/rfc/67.html


Author

Brian Hann (c0bra@users.sourceforge.net)

Copyright ©2000-2005, YaBB 3 Development Team. All Rights Reserved.
You may distribute this module under the terms of YaBB 3.
 YaBB3::BaseHash - A database record backed object.