I hesitate to answer as this is my first post to PM, but I've had similar problems in the past and think I can help. In an effort to avoid hand-coding SQL statements, I've opted for the following:
package Node; use base 'My::Class::DBI'; ... # standard Class::DBI stuff here # Declare that we have a relationships table that # tells us where our children are Node->has_many('children_refs' => 'Relationship' => 'parent_id'); # Now that we can fetch those records from the # relationships table, we need to have a way to # get Node objects from those relationships sub children { return Node->search_in( Node->primary_column => [ map { $_->child_id } shift->children_refs ] ); } # Similarly for parents
Now you'll need that search_in method as well.
package My::Class::DBI; use base 'Class::DBI'; sub search_in { my $class = shift; my %args = ref $_[0] eq 'HASH' ? ${ $_[0] } : @_; # Normalize columns my @columns = keys %args; $_ = $class->_normalized($_) for @columns; # Check columns $class->_check_columns(@columns); # Keep list of all values (for each column) # that'll be used in the IN clause -- we'll # later bind these values to an SQL statement # so that the DBI can take care of the quoting my @values; my $sql = join ' AND ', map { # Grab values for IN clause my @vals = @{ $args{$_} }; push @values, @vals; my $in_str = join(',', ('?') x @vals); "$_ IN ($in_str)" if @vals; } @columns; return unless @values; return $class->retrieve_from_sql($sql, @values); }

With the exception of the search_in method, this just might be a nice and tidy solution. I especially prefer this strategy as it doesn't rely on hand-coded SQL satements.

Good luck!
David


In reply to Re: Hierarchial relationships in Class::DBI by Anonymous Monk
in thread Hierarchial relationships in Class::DBI by Molt

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.