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.
Update: added a necessary "belongs_to."# 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); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: message board thread quandary
by stonecolddevin (Parson) on Sep 07, 2007 at 02:47 UTC | |
by Your Mother (Archbishop) on Sep 07, 2007 at 04:58 UTC | |
by stonecolddevin (Parson) on Sep 07, 2007 at 05:04 UTC | |
by Your Mother (Archbishop) on Sep 08, 2007 at 08:42 UTC | |
by stonecolddevin (Parson) on Sep 09, 2007 at 00:54 UTC |