[Dbix-class] multi-way relationships best practise
Carl Franks
fireartist at gmail.com
Fri Mar 3 12:58:04 CET 2006
I have a table of events, and another table of parent relationships, as below.
package cdiary::Schema::Event;
__PACKAGE__->table('events');
__PACKAGE__->add_columns(qw/ id title /);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->might_have( parent => 'Event_rel', {'foreign.id' => 'self.id'} );
package cdiary::Schema::Event_rel;
__PACKAGE__->table('event_rel');
__PACKAGE__->add_columns(qw/ id parentid /);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->belongs_to( parent => 'Event', { 'foreign.id' =>
'self.parentid' } );
An event may have an entry in the 'rel' table, stating which other
event is it's parent.
(The purpose is for repeat appointments)
What I'd like to know is, will it cause me any problems from a
DBIx-Class point-of-view if I add another relationship to the event
schema, like so:
__PACKAGE__->might_have( child => 'Event_rel', {'foreign.parentid' =>
'self.id'} );
So it has both might_have(child) and might_have(parent)
This way, I could do both
$event->find_related( 'parent' );
and
$event->find_related( 'child' );
rather than having to do
$event->find_related( 'parent' );
and
@rel = my::Event_rel->search({
parentid => $event->id,
});
my::Event->find( map {$_->id} @rel );
Will DBIx-Class choke when I start adding/deleting relationships?
Is there a better practise I could use instead?
Cheers,
Carl
More information about the Dbix-class
mailing list