Self-referencing tables

[prev] [thread] [next] [Date index for 2005/02/24]

From: simon jones
Subject: Self-referencing tables
Date: 10:54 on 24 Feb 2005
Hello all,

I'm just getting started with Class::DBI and it's pretty damn cool -
thank godness for the wiki it's a source of inspiration. I was reading
the self-referencing table section and applied what it said to my
application, but it doesn't seem to be exhibiting the behaviour I
would expect. Take for instance this -

$cat = category->create({ name => 'foo'});
$child = $cat->create_child({name => 'baz'});

(I created a method in category to create a child which looks like this)

sub create_child {
   my $self = shift;
   $_[0] ne 'HASH' and return undef;
   my $vars = $_[0];
   $vars->{parent_id} = $self->myid;
   return category->create($vars);
}

This all seems to work hunky dory. It's only when I try and do 
$child->parent_id->name;  that things fall apart.  Am I doing this
correctly?  Does anyone have a work-around that would allow me to
perform this operation?

thanks in advance. Si



- my schema looks like -
category->columns(Primary=> qw/myid fk_id/);
category->columns(Other=> qw/name parent_id/);
category->has_a(parent_id=> 'category');

Self-referencing tables
simon jones 10:54 on 24 Feb 2005

Re: Self-referencing tables
William Ross 11:45 on 24 Feb 2005

Generated at 17:56 on 01 Mar 2005 by mariachi v0.52