RE: No way to instantiate two related CDBI classes at once, isthere?

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

From: Perrin Harkins
Subject: RE: No way to instantiate two related CDBI classes at once, isthere?
Date: 18:29 on 10 Aug 2004
On Tue, 2004-08-10 at 11:53, Ian McDonald-ONLINE wrote:
> I'm not so bothered about avoiding the need to create an accessor; the
> method which creates both objects will sit outside either.
> 
> But I can't get it to work.

Probably because I got the construct() call wrong.  It should be a hash
ref:

sub retrieve_with_bar {
    my ($class, $id) = @_;
    my $sth = $class->sq_get_both();
    $sth->execute($id);
    my $row = $sth->fetchrow_arrayref();
    my $foo = Foo->construct({
                              id    => $row->[0],
                              other => $row->[1],    
                             });
    my $bar = Bar->construct({
                              id    => $row->[2],
                              other => $row->[3],    
                             });
    $foo->{'_bar_object'} = $bar;
    return $foo;
}

Be sure to name the columns correctly.

> I've tried calling $class->sth_to_objects($sth) or $class->construct
> repeatedly for each class, and although the first resulting object is
> fine, the second doesn't contain all the fields from the initiating hash
> - least of all its primary key.

sth_to_objects() uses the column names (from DBI) to know where to put
things, so that isn't going to work.  That's why I used construct()
instead.

> It seems to be pulling a broken something out of Live_Objects.

I think that's just a red herring.  The bad construct() call is the
problem.

> 526:            unless (defined($obj = $Live_Objects{$obj_key})) {
>   DB<5> p $obj_key
> Conquer::Persist::ActiveRecord::PC|cha_id=55
> 
>   DB<7> p %Live_Objects
> 
>   DB<11> p Dumper(%Class::DBI::Live_Objects)

It's not a global, it's a closure var.

>   DB<12> p $Live_Objects{$obj_key}
> Use of uninitialized value in print at (eval
> 83)[/usr/local/perl-5.6.1/lib/perl5/5.6.1/perl5db.pl:1521] line 2.

Doesn't that auto-vivify?  I think you will cause it to pass the
defined() test by doing this.

- Perrin

RE: No way to instantiate two related CDBI classes at once, isthere?
Perrin Harkins 18:29 on 10 Aug 2004

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