Re: loading many objects from one query

[prev] [thread] [next] [Date index for 2004/09/14]

From: Perrin Harkins
Subject: Re: loading many objects from one query
Date: 23:22 on 14 Sep 2004
On Tue, 2004-09-14 at 19:05, Andrew Pimlott wrote:
> Thanks for the ideas and example.
> 
> >     my $row = $sth->fetchrow_arrayref();
> >     my $foo = Foo->construct({
> >                               id    => $row->[0],
> >                               other => $row->[1],
> >                              });
> 
> It seems that in most cases, you should be able to do
> 
>     my $row = $sth->fetchrow_arrayref();
>     my $foo = Foo->construct($row);
>     my $foo = Bar->construct($row);

Only if there is no overlap in column names between the classes.

> (Probably there are some cases, eg with an outer join, when it's not so
> simple.)  For those cases, it "would be nice" if you could simply tell
> Class::DBI, build a Foo, a Bar, and a Baz for every row returned by this
> query, and give me back the (unique) Foos.

You might be able to establish a naming convention for columns that
would allow this (e.g. all columns for Foo are Foo.*), but there's only
so far you can go because you are writing this SQL by hand.  It's a
special case by definition.  I suppose you could create a way to
explicitly specify which columns turn into which objects.

> >     $foo->{'_bar_object'} = $bar;
> 
> But how to automate this... ?  IOW, I'd like to be able to tell
> Class::DBI, in a structured way, that every Foo "has_a" the Bar from the
> same row, or the Bazs comprise the "has_many" of the Foos.

Same as above: you can automate it if you have some way of knowing which
columns turn into which objects.

- Perrin

(message missing)

loading many objects from one query
Andrew Pimlott 01:05 on 14 Sep 2004

Re: loading many objects from one query
Perrin Harkins 02:22 on 14 Sep 2004

Re: loading many objects from one query
Tim Bunce 09:09 on 14 Sep 2004

Re: loading many objects from one query
Perrin Harkins 14:21 on 14 Sep 2004

Re: loading many objects from one query
Tim Bunce 16:07 on 14 Sep 2004

Re: loading many objects from one query
Perrin Harkins 16:12 on 14 Sep 2004

Re: loading many objects from one query
Andrew Pimlott 23:05 on 14 Sep 2004

Re: loading many objects from one query
Perrin Harkins 23:22 on 14 Sep 2004

Re: loading many objects from one query
Todd Lorenz 16:13 on 14 Sep 2004

Re: loading many objects from one query
Tim Bunce 13:52 on 15 Sep 2004

Re: loading many objects from one query
Perrin Harkins 15:22 on 15 Sep 2004

Re: loading many objects from one query
merlyn (Randal L. Schwartz) 17:10 on 16 Sep 2004

Re: loading many objects from one query
Andrew Pimlott 19:17 on 16 Sep 2004

Re: loading many objects from one query
Tim Bunce 17:36 on 14 Sep 2004

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