Threaded posts with a single table

[prev] [thread] [next] [Date index for 2004/08/04]

From:
Subject: Threaded posts with a single table
Date: 21:10 on 04 Aug 2004
Is it possible to use CDBI this way?

Given a table like:
 CREATE TABLE post
   (
    id     INT(9) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    parent INT(9),  # <-- this is just another post.id
    text   TEXT,
    etc    ...
    );

...then...

package My::Post;
use base qw( My::CDBI );

__PACKAGE__->table('post');
__PACKAGE__->columns(All => qw( id parent text etc ) );
__PACKAGE__->might_have(parent => __PACKAGE__);
__PACKAGE__->has_many(replies => __PACKAGE__);

So, the "parent" I can see working fine. It's just pointing to a "foreign"
id in its own table. It's the "replies" that obviously won't work that
way. Is there some way to do this automatically or would something like
the following be the way to go:

sub replies {
  my $self = shift;
  my $id = $self->id;
  return $self->retrieve_from_sql(qq{parent = $id});
}
sub add_to_replies {
  # something, something
}

Has anyone already done this in a better way?

Thanks!
-Ashley



Threaded posts with a single table
21:10 on 04 Aug 2004

Re: Threaded posts with a single table
Simon Cozens 21:14 on 04 Aug 2004

Re: Threaded posts with a single table
Perrin Harkins 21:21 on 04 Aug 2004

Re: Threaded posts with a single table
Ian Holsman 21:45 on 12 Aug 2004

Generated at 11:35 on 01 Dec 2004 by mariachi v0.52