Hans Dieter Pearcey wrote:
On Wed, May 27, 2009 at 04:40:37PM -0400, Dimitri Ostapenko wrote:
So using a role to model lower-level table I need to be able to tell
which attributes come from class and which come from role in methods
for saving and retrieving data.
Maybe you want a trait for your attributes that lets you store a table per
attribute definition, or something?
hdp.
!DSPAM:2337,4a1da61670674007936253!
Let's say we have table 'family' and table 'family_member'. Family has
attributes:
- size
- family name
- kind
family_member has:
- sex
- first_name
- age
For a family of 3 there will be 3 rows in 'family_member' for 1 row in
'family'.
package FamilyMember;
use Moose::Role;
use MY::Types;
has 'id' => (is => 'rw', isa => 'PosInt' );
# serial
has 'sex' => (is => 'ro', isa => 'SexType',required=>1);
has 'first_name' => (is => 'ro', isa => 'Str',required=>1 );
has 'age' => (is => 'ro', isa => 'PosInt', default=> 0 );
has 'family_id' => (is => 'ro', isa => 'PosInt', required=>1 ); #
foreign key
package Family;
use Moose;
use MY::Types;
with qw(MY::Base MY::FamilyMember );
has 'id' => (is => 'rw', isa => 'PosInt' );
has 'size' => (is => 'ro', isa => 'PosInt', required=>1);
has 'family_name' => (is => 'ro', isa => 'Str', required=>1 ); #
Unique within a table
has 'kind' => (is => 'ro', isa => 'FamilyKind', default=> 'Traditional' );
sub _table { 'family'};
sub _base_table { 'family_member'};
# Return 3 rows from base table, table
sub get {
my (%arg) = @_;
my $id = $arg{id};
my $family_name;
my ($rows,$where);
croak "'id' or 'family_name' is required" unless ($id || $family_name);
if ( $id ) {
$where = "id='$id'";
} else {
$where = "family_name='$family_name'";
}
my $qry = 'SELECT * FROM '. _table().' a,'. _base_table().' b WHERE
a.id=b.family_id AND '.$where;
my $rows = MY::DB::select (sql => $qry, returns => 'hash');
return $rows
}
sub save {
# Need to know here which attr comes from where to be able to save into
correct table
}