Re: [CDBI] Why does CDBI not populate the object on insert
Bill Moseley <[email protected]> Thu, 5 Mar 2009 15:03:09 -0800
| Newsgroups | gmane.comp.lang.perl.modules.class-dbi |
|---|---|
| Message-ID | <[email protected]> |
On Thu, Mar 05, 2009 at 02:15:40PM -0800, Michael G Schwern wrote:
> >>
> >> 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.
>
> Well, keep in mind that it's not *another* fetch of the database as no fetch
> should have been done by that point unless it's to get the auto incremented
> primary key which is cheap.
Ya, but my point of course is if I just defined the artist name
I probably don't need to fetch it back from the database.
> > That's exactly the reason. Triggers (both the database kind and possibly the
> > CDBI kind) might change the record values after it is inserted into the
> > database. The primary key columns should be intact, I believe, but the other
> > columns are flushed and all "essential" columns reloaded upon access.
>
> Exactly right. Don't forget about defaults and truncation by type. For
> example, DECIMAL(6,2) or CHAR(2). It's more common than you'd think.
Yes, although I'd be hard pressed to think of a time when:
$foo->value( $new_value );
$foo->update;
$foo->value ne $new_value;
But, yes, have to be careful. I could put a flat in each class to say
when it was safe to do that, perhaps.
> You can mitigate the problem by not using lazy loading, just set all columns
> to "All", then you just have the one select.
I did try and customize my Essential. I think I have some code that
overrides search() that lets me know when CDBI is going back to the db
for extra columns -- that was helpful in tuning years back.
Again, what I noticed was a long running script that was creating a
bunch of nested structures and then the trip backwards was generating
quite a few selects for object I knew I had.
$country = $hood->city->state->country;
> To turn it off entirely, you can override the create trigger. A hack, but a
> simpler hack than rewriting _insert(). It might be worth making it a
> configurable option.
Thanks, I'll look at that. I was thinking of overriding insert() and
then stuffing any blessed hash elements back into the new object.
Might be more involved than that.
Thanks,
--
Bill Moseley.
[email protected]
Sent from my iMutt