[CDBI] Why does CDBI not populate the object on insert

Bill Moseley moseley at hank.org
Thu Mar 5 18:14:41 GMT 2009

This list is pretty quiet these days.

I'm looking at some old code and feeling a bit rusty with CDBI and
wondering about something.  My guess is I've just forgot the reasoning

If I do:

    my $artist = Artist->insert({ name => 'Artist Name' });

then $artist->name doesn't exist yet in the object.  That means, of
course, that $artist->name must do another fetch of the database.

I suppose there could be times when "name" comes back out of the
database different than it went in, but most of the time not.
Just seems a bit inefficient.

Actually, where I noticed this is with deeply nested tables.  For
example, I have a $world object:

    $country = Country->insert({ world => $world } );
    $state   = State->insert( { country => $country } );
    $city    = City->insert( { state => $state } );
    $hood    = Hood->insert( { city => $state } );

Then later, this ends up doing a select for each one, even though
we have the objects for each one.

    my $country = $hood->city->state->country;

In other words, I wonder what would break if after _insert() was
called the object got set.  That is, effectively:

    $country = Country->insert({ world => $world } );
    $country->{world} = $world;

Bill Moseley
moseley at hank.org
Sent from my iMutt

More information about the ClassDBI mailing list