Re: A trigger called when following a 'has_a'

[prev] [thread] [next] [Date index for 2004/07/20]

From: Charles Bailey
Subject: Re: A trigger called when following a 'has_a'
Date: 15:22 on 20 Jul 2004
--On Tuesday, July 20, 2004 3:53 PM +0100 Peter Pimley 
<ppimley@xxxxxxxxx.xxx> wrote:

> Charles Bailey wrote:
>
>> Unless you want to rearrange the Location object somehow, you
>> shouldn't need a separate trigger.
>
> Thanks for the quick reply.
>
> Rearranging the Location object is exactly what I want to do :)
>
> What I want to do (and this is a bit nasty) is to immediately bless the
> object into a subclass of Location.  Location is actually an abstract
> class.  Exactly which subclass I want to bless it to is stored as a
> column of the table.  Unpleasant I admit, but I couldn't find anything
> online to recommend a nicer way of doing this sort of thing.

I haven't found a better way for a similar case here, either.  Here's what 
I use:

In class List, the Origin attribute is an object whose OID is stored in 
List's "origin" column, and whose type (one of several subclasses of the 
generic Origin base class) is stored in the List's "origin_type" column. 
When setting up List, I declare the relationship as follows:

 $class->has_a(Origin => 'Origin',
		       inflate => sub { shift; Origin->inflate(@_); },
	           deflate => sub { my($ori,$list) = @_;
				                $list->_attribute_store($otype,
						        $ori->origin_type)  if ref $list;
			                    $ori->id->internal; });

and the Origin base class provides the inflate() method to its kids:

  sub inflate {
    my($id,$parent,$otype) = @_;
    $otype = '' . $parent->find_column('Origin_type') unless defined $otype;
    my $target = $Otype_to_class{$parent->_attrs($otype)};
    $target->_simple_bless($id);
  }

where $Otype_to_class is a mapping in which each subclass registers itself 
as it's loaded.  It's ugly, and perhaps more contorted than you need, but I 
hope it provides some guidance as to how the CDBI relationship might be set 
up in this sort of situation.

--
Regards,
Charles Bailey  < bailey _at_ newman _dot_ upenn _dot_ edu >
Newman Center at the University of Pennsylvania

(message missing)

Re: A trigger called when following a 'has_a'
Peter Pimley 14:17 on 20 Jul 2004

Re: A trigger called when following a 'has_a'
Charles Bailey 14:42 on 20 Jul 2004

Re: A trigger called when following a 'has_a'
Peter Pimley 14:53 on 20 Jul 2004

Re: A trigger called when following a 'has_a'
Charles Bailey 15:22 on 20 Jul 2004

Re: A trigger called when following a 'has_a'
Charles Bailey 15:30 on 20 Jul 2004

Re: A trigger called when following a 'has_a'
Peter Pimley 15:37 on 20 Jul 2004

Re: A trigger called when following a 'has_a'
Charles Bailey 15:47 on 20 Jul 2004

Re: A trigger called when following a 'has_a'
Peter Pimley 10:32 on 21 Jul 2004

A trigger called when following a 'has_a' (again)
Peter Pimley 13:41 on 22 Jul 2004

Generated at 11:34 on 01 Dec 2004 by mariachi v0.52