Re: Self-referencing tables

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

From: William Ross
Subject: Re: Self-referencing tables
Date: 11:45 on 24 Feb 2005
On 24 Feb 2005, at 10:54, simon jones wrote:

> 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;

that should be ref($_[0]), no? and it's usually better to use a plain 
return; rather than returning undef, as (undef) is true in list 
context.

>    my $vars = $_[0];
>    $vars->{parent_id} = $self->myid;

the next step would be to add a debugging line here. something like:

	warn Dumper($vars);

will tell you what is going into the create statement.

>    return category->create($vars);

and another one here:

	my $child = $self->create($vars);
	warn Dumper($child);
	return $child;

will let you compare that to the object that has been constructed.

but it's most likely that your problem is here:

> category->columns(Primary=> qw/myid fk_id/);

the category class has a multiple-column primary key, but you're using 
only the myid column to form a relationship to it. It's likely that 
$child->parent_id is being stored, but retrieving the parent object 
using only that value is failing. If you really need to have the 
two-column key, then Perrin has the answer:

http://www.spanner.org/lists/cdbi/2004/09/03/d35a4910.html

best

will

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