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

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

From: Ian McDonald-ONLINE
Subject: RE: No way to instantiate two related CDBI classes at once, isthere?
Date: 15:53 on 10 Aug 2004
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.

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. There's a large chance that I'm missing
something, but it looks to me that something's going wrong inside
Class::DBI::_init.

It seems to be pulling a broken something out of Live_Objects. Despite
the fact that there's nothing in Live_Objects. From the Perl debugger:

526:            unless (defined($obj =3D $Live_Objects{$obj_key})) {
  DB<5> p $obj_key
Conquer::Persist::ActiveRecord::PC|cha_id=3D55

  DB<7> p %Live_Objects

  DB<11> p Dumper(%Class::DBI::Live_Objects)

  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.

  DB<13> p $obj
Use of uninitialized value in print at (eval
84)[/usr/local/perl-5.6.1/lib/perl5/5.6.1/perl5db.pl:1521] line 2.

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

  DB<15> p defined($obj =3D $Live_Objects{$obj_key})

  DB<16> p $obj
Use of uninitialized value in print at (eval
87)[/usr/local/perl-5.6.1/lib/perl5/5.6.1/perl5db.pl:1521] line 2.

  DB<17> s
Class::DBI::_init(/home/ianm/conquer/cgi-bin/lib/Class/DBI.pm:542):
542:            return $obj;
  DB<17> p $obj
Conquer::Persist::ActiveRecord::PC
  DB<18> p Dumper($obj)
$VAR1 =3D bless( {
                 'sso_user_id' =3D> undef,
			... (partial list of fields, none of which have
true values, deleted)
               }, 'Conquer::Persist::ActiveRecord::PC' );

  DB<19>=20

-----Original Message-----
From: Perrin Harkins [mailto:perrin@xxxx.xxx]=20
Sent: 09 August 2004 16:56
To: brano@xxxxxxxxxx.xx
Cc: cdbi-talk@xxxxxx.xxxxx.xxx
Subject: Re: No way to instantiate two related CDBI classes at once,
isthere?


It can be done.

package Foo;

__PACKAGE__->set_sql(get_both =3D> qq{
    SELECT foo.id, foo.other, bar.id, bar.other
    FROM __TABLE__
    WHERE foo.a =3D ?
};

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

sub bar {
    my $self =3D shift;
    return $self->{'_bar_object'};
}

If you read through the Class::DBI::Relationship::MightHave source, you
can see how to make it store the object just like the normal might_have
method does, and avoid the need to create your own accessor.  That would
certainly violate encapsulation, and my example here has
retrieve_with_bar tightly coupled to the SQL statement, but it should
work.

- Perrin


http://www.bbc.co.uk/ - World Wide Wonderland

This e-mail (and any attachments) is confidential and may contain
personal views which are not the views of the BBC unless specifically
stated.
If you have received it in error, please delete it from your system.=20
Do not use, copy or disclose the information in any way nor act in
reliance on it and notify the sender immediately. Please note that the
BBC monitors e-mails sent or received.=20
Further communication will signify your consent to this.

RE: No way to instantiate two related CDBI classes at once, isthere?
Ian McDonald-ONLINE 15:53 on 10 Aug 2004

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