[Dbix-class] Bidirectional many-to-many on a single table?

Jess Robinson castaway at desert-island.me.uk
Mon Mar 31 13:32:22 BST 2008



On Tue, 25 Mar 2008, Alex Peters wrote:

> Hi,
>
> I'm struggling to convey a many-to-many relationship on the
> same table with DBIx::Class.  I've read over all of the
> relevant documentation on CPAN and I've read past posts to this
> list that seemed closely relevant by subject[1].
>
> I have a database table called CriterionConflict with two
> columns, criterionA and criterionB.  This table intends to
> document instances of Criterion records that cannot coexist.
> The conflicts are bidirectional; there is no meaning to the
> ordering of a pair of IDs in a CriterionConflict record.
>
> I think a 'conflicts' accessor/mutator method in the Criterion
> ResultSource class would be ideal, but I don't know how I might
> go about this.  For the accessor side of things I imagine I'd
> need to construct a ResultSet searching across both columns;
> for the mutator I'd probably start out with something similar.

To get all conflicts for a criteria, regardless of order, you can add a 
relationship something like:

__PACKAGE__->has_many('conflicts', 'Schema::CriterionConflict',
                       [ { 'foreign.criterionA' => 'self.id' },
                         { 'foreign.criterionB' => 'self.id'} ]);


Which is a condition with an OR clause.

> I'm also unsure as to whether I need a CriterionConflict
> ResultSet class, since I can't see any value to making this
> instantiable.

You never need one unless you have extra methods for the resultset.
Or did you mean a result class? You cant not have one of those ;)




More information about the DBIx-Class mailing list