Re: Threaded posts with a single table
[prev]
[thread]
[next]
[Date index for 2004/08/12]
jinx@xxxxxxx.xxx wrote:
> # sorry, Perrin, didn't reply all the first time (don't use this client
> usually, sigh)
>
>>On Wed, 2004-08-04 at 17:10, jinx@xxxxxxx.xxx wrote:
>>
>>>__PACKAGE__->might_have(parent => __PACKAGE__);
>>>__PACKAGE__->has_many(replies => __PACKAGE__);
>>
>>I would change that might_have to this:
>>__PACKAGE__->has_a(parent => __PACKAGE__);
>
>
> Sure thing. I had it that way first but wasn't sure since a post could be
> original (parentless).
>
>
>>I also like the explicit form of has_many instead of the magical form
>>you're using there:
>>__PACKAGE__->has_many(replies => __PACKAGE__, 'parent');
>>
This snippet might also be useful to you.. It pulls the entire tree both
ways
#
# this application's parents
sub parents($) {
my $self=shift;
my @application=@_;
my @parents;
my @parentspassed;
my %parents;
push @parents, $self;
while (@parents) {
my $pid=pop @parents;
my $parent = $pid->application_parent;
if (defined($parent)) {
next if ($parents{$parent->id}) ;
$parents{$parent->id} = $parent;
push @parents, $parent;
push @parentspassed, $parent;
}
}
return reverse @parentspassed;
}
#
# this applications children
# (so you can figure out what needs updating)
sub children($) {
my $self=shift;
my @application=@_;
my @children;
my @childrenpassed;
my %children;
push @children , $self;
while (@children) {
my $pid=pop @children;
my @childs =
Nagios::Application->search({application_parent=>$pid});
foreach my $child (@childs) {
next if ($children{$child->id});
push @children, $child;
push @childrenpassed, $child;
$children{$child->id} = $child;
}
}
return @childrenpassed;
}
#
>>
>>>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.
>>
>>What makes you think that?
>
>
> The relative density of my gray matter, I fear. CDBI relations can be a
> bit magical/confusing to me. That looks great if it does what I think it
> does. I also noticed that the POD for CDBI is much fuller now than it was
> a few versions back (or else I'm just understanding more now) so I have
> some reading to catch up on.
>
> Thanks for the help. And to Simon, of course!
> http://search.cpan.org/search?query=Email%3A%3AStore%3A%3AThread&mode=all
> is very interesting code.
>
> -Ashley
>
>