rootcho has asked for the wisdom of the Perl Monks concerning the following question:

hi,
With DBIx::Class I was doing this :
$rs->field2($r->field2 + 1); $rs->update;
Is there a way to do this directly in an update like this :
$rs->update({field1 => 55, field2 => 'field2+1' }); or $rs->update_or_create({field1 => 55, field2 => 'field2+1' });
The above code doesn't work, I posted it just like an example. I hope also the second example to generate just one UPDATE, right ? Not sure for the first one, though !

Replies are listed 'Best First'.
Re: DBIx update and simultaneosly modify field
by jasonk (Parson) on Jun 26, 2007 at 22:38 UTC

    DBIx::Class will pass scalar refs as literal SQL, so you can do this...

    $rs->find_or_create({ field1 => 55 })->update({ field2 => \'field2+1' });

    We're not surrounded, we're in a target-rich environment!
Re: DBIx update and simultaneosly modify field
by stonecolddevin (Parson) on Jun 26, 2007 at 21:47 UTC

    How's is the above code "not working"? I'm pretty sure as long as your resultset isn't empty $rs->update({field1 => 55, field2 => 'field2+1' }); will work.

    Now, if you mean 'field2+1' isn't working, you may want to use something like

    my $field2 = $rs->field1 + 1; $rs->update({field1 => 55, field2 => $field2 });
    (Untested).

    HTH!

    meh.
Re: DBIx update and simultaneosly modify field
by snoopy (Curate) on Jun 26, 2007 at 23:49 UTC
    You can set cdbi autoupdate on an object to enable immediate database updates
    $rs->autoupdate(1); $rs->set(field1 => 55, field2 => 'field2+1');
    Update: You'll also need to enable the cdbi auto-update facility in your class set-up:
    package MyClass; use base qw(DBIx::Class) __PACKAGE__->load_components(qw/CDBICompat::AutoUpdate/); #....
      my mine concern is not does it do autoupdate or not, but will it generate 1 or 2 queries ? thanx
      I think there was some way to debug this !!! DBIx::Storage or something..hmmm let me check