From: Michael G S. <sc...@po...> - 2001-04-21 19:47:01
|
On Sat, Apr 21, 2001 at 03:22:09PM -0400, Raul Nohea Goodness wrote: > Michael G Schwern wrote: > > I've run into a snag in Class::DBI's interface. When I first wrote > > Well, using common and/or vague method names for your classes can be > bad. However, isn't it also bad to subclass a class and not honor the > interface? Say Class::DBI used create() and retrieve()... package My::Class::DBI; use base qw(Class::DBI); sub new { my $class = shift; $class->create(@_); } This is still honoring the Class::DBI interface. My::Class::DBI->create(...) will work just fine. My::Class::DBI->new(...) is just a synonym. What the subclasser must be careful of is that if it wishes to override it must do it in create() not new. So you'd want to do this... package My::Class::DBI; use base qw(Class::DBI); sub new { my $class = shift; $class->create(@_); } sub create { print "HEY EVERYBODY, I'M CREATING AN OBJECT!" $class->SUPER::create(@_); } not this... package My::Class::DBI; use base qw(Class::DBI); sub new { my $class = shift; print "HEY EVERYBODY, I'M CREATING AN OBJECT!" $class->create(@_); } > I have to say that when i started using Class::DBI for a project, i was > initially surprised that new() instantiated the object and did an insert > in the database. There can be needs to instantiate an object before > persisting it to the database. Yes, it does this because 1) often tables will have rows with defaults that the database will fill in and more importantly 2) if you're using a sequence or AUTO_INCREMENT primary key its the only way to get the ID for the object. I recently documented the construct() method which is *almost* what you want. It can create an object without inserting, but it assumes the object is already in the database. I could add in a method to do what you want (you're not the first to ask for it), but I'd have to think about it for a bit first. A Class::DBI object that doesn't have a corresponding row isn't fully functional, so I may need a special subclass for them. PS Is dbi_commit() working for you? -- Michael G. Schwern <sc...@po...> http://www.pobox.com/~schwern/ Perl6 Quality Assurance <pe...@pe...> Kwalitee Is Job One BOFH excuse #75: There isn't any problem |