Re: A trigger called when following a 'has_a' (again)
[prev]
[thread]
[next]
[Date index for 2004/07/22]
--On Thursday, July 22, 2004 2:41 PM +0100 Peter Pimley
<ppimley@xxxxxxxxx.xxx> wrote:
> So, back to my original problem: There is no trigger that gets called on
> an object before it is returned from a 'has_a' method. The docs for
I think the issue isn't so much that no trigger gets called -- for
instance, has_a()s are implemented as triggers at the moment -- but that
there's no guaranteed order of evaluation for triggers. I suspect in your
case that the bless_to_subclass() trigger end up being called before the
inflator() trigger that's part of the has_a, which happily reblesses the
object back into the superclass. Since it's in a grey area, I guess it's
not really a bug, though one can argue that the current behavior (if random
trigger order hands you an object already blessed into a subclass of the
has_a's "target" class, rebless it into the target class) is less intuitive
than just letting it go by. That said, changing C:D:R:HasA to pass the
already-blessed object shouldn't break anything that (reliably) works now,
and would let an arrangement like your work reliably. That's attractive to
me, since only the superclass needs to know about the magic relationship
with its subclasses.
In the meantime, custom inflators aren't subject to this problem, so you
can avoid it by saying
__PACKAGE__->has_a(my_thing => 'superclass',
inflate => \&bless_to_subclass);
(In order to explain the last call in your example, I'm making the further
inference here that $owner owns my_thing #1 or #2, so that [courtesy of the
Live_Objects cache] when one of the later retrieve()s calls just the
bless_to_subclass() trigger, the single common instance of my_thing is
reblessed into the right subclass. If you retrieve() another owner of the
same my_thing, does it revert to the superclass? Alternatively, if you
place the retrieve() call for $owner after the retrieve() calls for $sub or
only retrieve $subs not owned by $owner, does $owner->my_thing->do_stuff
call the superclass method?)
Please pardon the rampant speculation here. If I'm off base, I'll have
some time this evening or tomorrow to look at this in the debugger and try
to sort it out.
> has_a say that:
>
> "If the foreign class is another Class::DBI representation we
> will call retrieve() on that class with our value."
>
> But this is not exactly what happens. I can do a retrieve and my trigger
It's not, but it's fairly close. Other than trigger order issues, the
only real limitation is that a method called on the target object can't
assume that any attributes other than the key have been fetched. However,
that's also true of a straight retrieve(), so it shouldn't cause major
coding headaches.
--
Regards,
Charles Bailey < bailey _at_ newman _dot_ upenn _dot_ edu >
Newman Center at the University of Pennsylvania
|
(message missing)
|