[CDBI] how to generate a virtual column object from more that one real columns

Alan alan at ufies.org
Sat Mar 10 22:34:47 GMT 2007


On Sat, Mar 10, 2007 at 03:29:41PM -0600, Mike Demoulin wrote:
> Here's what I want to do. I have a table (person) that has, among other
> columns, fname, mname, and lname. I have a Class::DBI subclass (Person) to
> handle that table. I have a class (Name) to encapsulate those 3 values into
> one object. Instead of constructing the Name form Person after each query, I
> would like Person to be able to treate name as a column, and to return a
> Name.

I can think of two solutions:

1 - create a 'name' method in C::Person that does the C::Name lookup.

IE: 
package C::Person;
use blah;
[ set up tables, etc]

sub name {
        my $self = shift;
        my $name = C::Name->search( { person_id => $self->id } );
        return $name; # to return the object or
        return $name->first . " " . $name ->last; # to return string
}

2 - Use view (aggregate table) (postgres has these) to combine the two
tables.


-- 
Alan <alan at ufies.org> - http://arcterex.net
--------------------------------------------------------------------
"Backups are for people who don't pray."                 -- big Mike



More information about the ClassDBI mailing list