in reply to Hierarchial relationships in Class::DBI
Now you'll need that search_in method as well.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
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
|
|---|