To do an insert with Class::DBI, you can simply do:
my $object = Object::DB->insert({ a => 1, b => 2, c => 3, ...});
But there is no such thing for update. The best I could come up with is selecting the record first then updating it:
my $object = Object::DB->retrieve($id);
my $object->set( a => 1, b => 2, c => 3, ...};
$object->update;
This is not efficient since I have to do a SELECT first, and then an UPDATE instead of just one UPDATE.
Is there a better way to do this with Class::DBI? I don't want to do 42 $object->a(1), $object->b(2), etc., $object->update;
As far as I know, Class::DBI does not have a good way to do this, As you've noted, its
update()method is meant to be called on an object that has been previously loaded from the database.You may be able to convince Class::DBI to do what you want, however, with something like this:
If this feature is important to you and you're not already too far into your project, you will definitely have better luck with one of the newer Perl ORMs such as Rose::DB::Object or DBIx::Class. DBIx::Class even includes a Class::DBI compatibility layer.