NAME
ClearPress::model - a base class for the data-model of the ClearPress MVC family
VERSION
$Revision: 470 $
SYNOPSIS
use strict;
use warning;
use base qw(ClearPress::model);
__PACKAGE__->mk_accessors(__PACKAGE__->fields());
sub fields { return qw(...); }
DESCRIPTION
SUBROUTINES/METHODS
fields
my @aFields = $oModel->fields();
my @aFields = __PACKAGE__->fields();
primary_key - usually the first element of fields();
my $sPrimaryKey = $oModel->primary_key();
secondary_key - alternative key, usually a unique, non-numeric name to complement the primary_key
my $sSecondaryKey = $oModel->secondary_key();
table - database table name this class represents
my $sTableName = $oModel->table();
init - post-constructor hook, called by new();
new - Constructor
my $oInstance = ClearPress::model::subclass->new();
also supports creation with a hashref of key:values and creation
with a scalar primary key, e.g.
my $oInstance = ClearPress::model::subclass->new({id_subclass => 10});
my $oInstance = ClearPress::model::subclass->new(10);
util - ClearPress::util object
my $oUtil = ClearPress::model::subclass->util();
my $oUtil = $oInstance->util();
get - generic 'get' accessor, derived from Class::Accessor.
Invokes $self->read() if necessary.
my $sFieldValue = $oModel->get($sFieldName);
gen_getarray - Arrayref of objects of a given type for a given database query
my $arObjects = $oModel->gen_getarray('ClearPress::model::subclass',
q(SELECT a,b,c FROM x,y WHERE x.d=? AND y.e=?),
@bind_params);
gen_getall - Arrayref of all objects of type (ref $self) or a given class
my $arObjects = $oModel->gen_getall();
my $arObjects = $oModel->gen_getall('ClearPress::otherclass');
gen_getobj - An object of a given class based on the value of the primary key in that class equalling the value in the same field-name in this object.
my $oObj = $self->gen_getobj($sClass);
gen_getfriends - arrayref of relatives related by this model's primary key
my $arObjects = $oModel->gen_getfriends($sClass);
my $arObjects = $oModel->gen_getfriends($sClass, $sCacheKey);
gen_getfriends_through - arrayref of relatives related by this model's primary key through an additional join table
my $arObjects = $oModel->gen_getfriends($sClass, $sJoinTable);
my $arObjects = $oModel->gen_getfriends($sClass, $sJoinTable, $sCacheKey);
gen_getobj_through - fetch a relative through a join table
my $oRelative = $oModel->gen_getobj_through($sClass, $sJoinTable);
my $oRelative = $oModel->gen_getobj_through($sClass, $sJoinTable, $sCacheKey);
has_a - one:one package relationship
__PACKAGE__->has_a('my::pkg');
__PACKAGE__->has_a(['my::pkg1', 'my::pkg2']);
__PACKAGE__->has_a({method => 'my::fieldpkg'});
__PACKAGE__->has_a([{method_one => 'my::pkg1'},
{method_two => 'my::pkg2'});
has_many - one:many package relationship
__PACKAGE__->has_many('my::pkg');
If my::pkg has a table of "package" then this creates a method "sub
packages" in $self, yielding an arrayref of my::pkg objects related
by the primary_key of $self.
__PACKAGE__->has_many(['my::pkg1', 'my::pkg2']);
Define multiple relationships together.
__PACKAGE__->has_many({method => 'my::fieldpkg'});
Defines a method "sub methods" in $self yielding an arrayref of
my::fieldpkg objects related by the primary_key of $self.
__PACKAGE__->has_many([{method_one => 'my::pkg1'},
{method_two => 'my::pkg2'});
Defines multiple relationships with overridden method names.
hasa - deprecated synonym for has_a()
belongs_to - synonym for has_a()
hasmany - deprecated synonym for has_many()
has_many_through - arrayref of related entities through a join table
Define a 'users' method in this class which fetches users like so:
SELECT u.id_user, u.foo, u.bar
FROM user f, centre_user t
WHERE t.id_this = ? # the primary_key for $self's class
AND t.id_user = f.id_user # the primary_key for friend 'user'
__PACKAGE__->has_many_through(['user|centre_user']);
has_a_through - a one-to-one relationship, like has_a, but through a join table
__PACKAGE__->has_a_through(['user|friend', 'user|enemy']);
belongs_to_through - synonym for has_a_through
has_all - allows fetching of all entities of this type
__PACKAGE__->has_all();
create - Generic INSERT into database
$oModel->create();
read - Generic lazy-load from the database
$oModel->load();
update - Generic UPDATE into database against primary_key
$oModel->update();
delete - Generic delete from database
$oModel->delete();
save - Generic save object to database
$oModel->save();
zdate - Generic Zulu-date based on object's date() method or gmtime
my $sZuluTime = $oModel->zdate();
isodate - Generic iso-formatted date YYYY-MM-DD HH:MM:SS for gmtime
my $sISODate = $oModel->isodate();
as_json - JSON representation of this object
my $sJSON = $oModel->as_json();
as_xml - XML representation of this object
my $oXML = $oModel->as_xml();
DIAGNOSTICS
CONFIGURATION AND ENVIRONMENT
DEPENDENCIES
INCOMPATIBILITIES
BUGS AND LIMITATIONS
AUTHOR
Roger Pettett, <rpettett@cpan.org>
LICENSE AND COPYRIGHT
Copyright (C) 2008 Roger Pettett
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.4 or, at your option, any later version of Perl 5 you may have available.