in reply to message board thread quandary

If you're also using DBIx::Class, this half-pseudo code does the trick. I am running this for real (in three different versions, one is Class::DBI, two are Catalyst) in a couple production situations. Sorry I'm not posting the full working code but that would be a BIG amount of stuff including the DB and everything and I'm not quite ready to share it. I think I included most of the working bits. In has both posts that are threaded (like WordPress "pages," I think) and threaded comments belonging to the post.

# blog posts (also threaded) package MyApp::Schema::DB::post; use base qw/ DBIx::Class /; __PACKAGE__->load_components qw( PK::Auto Core ); __PACKAGE__->table('post'); # ... missing some regular set-up code __PACKAGE__->add_columns(qw/ id user parent title body golive created updated /); __PACKAGE__->belongs_to('parent' => 'MyApp::Schema::DB::post'); __PACKAGE__->has_many('children' => 'MyApp::Schema::DB::post' => 'pare +nt', { order_by => "golive" } ); # ... missing some other methods sub parents { my ( $self, @parents ) = @_; my $parent = $self->parent; return @parents unless $parent; unshift @parents, $parent; die "Unterminating lineage loop suspected!" if @parents > 50; $parent->parents(@parents); } # comments ... threaded, attached either to a # parent comment or directly to a post package MyApp::Schema::DB::comment; use base qw/ DBIx::Class /; __PACKAGE__->load_components qw( PK::Auto Core ); __PACKAGE__->table('comment'); __PACKAGE__->add_columns(qw/ id post user parent title body created updated /); __PACKAGE__->belongs_to('post' => 'Yesh::Schema::DB::post'); __PACKAGE__->belongs_to('parent' => 'MyApp::Schema::DB::comment'); __PACKAGE__->has_many('replies' => 'MyApp::Schema::DB::comment' => 'pa +rent'); sub depth { my $self = shift; return 1 + scalar $self->parents; } sub parents { my ( $self, @parents ) = @_; my $parent = $self->parent; return @parents unless $parent; push @parents, $parent; die "Unterminating lineage loop suspected!" if @parents > 100; $parent->parents(@parents); }
Update: added a necessary "belongs_to."

Replies are listed 'Best First'.
Re^2: message board thread quandary
by stonecolddevin (Parson) on Sep 07, 2007 at 02:47 UTC

    So you do use two tables with this set up?

    meh.

      Only because the posts are threaded too. It's not necessary to have another table otherwise (well, except for users and whatever you want attached). The comments are threaded in and of themselves. It just happens to be a blog package so they are also attached to a post. If it were for a forum, they'd be standalone in their table and any without a parent would be the first in a thread.

        I'm a little confused then, perhaps you could help me out and explain how I set up a relationship within the post/blog table to retrieve the replies to replies correctly as you've done in your code. I don't quite grok how that is done.

        meh.