[CDBI] ChildOf

Kate Yoak kate at yoak.com
Sat Sep 10 00:24:37 BST 2005

I am working on a relationship class which I have found very useful.  Wanted
to throw it out here for critique.

But before I overwhelm everyone with code, I'll explain the motivation.  If
there is interest in the code itself, I'll post it.

...::Relationship::ChildOf - child_of is a type of relationship that's
frequently encountered between parent/child entities:  company => employee;
advertiser => ad; cluster => server, etc.

In each of these pairs the first entity have many second entities; and the
2nd is a child_of first.

__PACKAGE__->child_of ( method_name => Class => foreign_key );

CDBI::Server->child_of ( cluster => CDBI::Cluster => 'cluster_id');

OR (you could be child of multiple clusters:)

CDBI::Server->child_of ( maintenance_cluster => CDBI::Cluster =>
CDBI::Server->child_of ( web_cluster => CDBI::Cluster => 'wcluster_id');
Like might_have, child_of creates the accessor (.e.g web_cluster) which will
return a parent object.

There is only one trigger that i could think of: before_update - we should
really update the parent with the child.  Nothing else appears appropriate
to me.

That's it.  It basically enables object hierarchy, like so:

$link->page->website->company->account :)
While that does seem a little silly (and is not really the intent, it
demonstrates the idea.  And here is another one of my favorites:

my @sites = $site->webmaster->sites( status => 'active' );
# find all the active sister-ads, the ones that come from the same
advertiser.  Here we utilize child_of & has_many as indicated above.

More information about the ClassDBI mailing list