New relatively new to OOP (I've danced with it a few times over the last couple of years but keep giving up after I get in over my head). I'm also very new to Class::DBI.

I'm having a problem. According to http://www.class-dbi.com/cgi-bin/wiki/index.cgi?AtomicUpdates, I need to use the following code and place it in (I think?) my subclass.

This is my subclass.
I'm not sure if do_transaction() belongs here or not? I think it does . . . ? And I can't figure out how to properly refer to my database handle from within do_transaction(), since do_transaction() refers to $dbh and I don't have a $dbh. In the main script, if I want to access my database handle, I do something like my $use = MyClass::DBI::User->retrieve($uid).

package MyClass::DBI; use strict; use base qw( Class::DBI::Pg ); __PACKAGE__->set_db(Main => 'dbi:Pg:dbname=sometable', 'xxxxxxx', 'xxx +xxx', { PrintError => 1, RaiseError => 1, AutoCommit => 1, ShowErrorStatement => 1, ChopBlanks => 1, } ); sub do_transaction { my($class,$code,@args) = @_; $class->_invalid_object_method('do_transaction()') if ref($class); my @return_values = (); my $dbh = $class->db_Main; # Localize AutoCommit database handle attribute # and turn off for this block. local $dbh->{AutoCommit}; # Note: Leaks memory with Perl 5.6.1. eval { @return_values = $code->(@args); $class->dbi_commit; }; if ($@) { my $error = $@; eval { $class->dbi_rollback; }; if ($@) { my $rollback_error = $@; $class->_croak("Transaction aborted: $error; " . "Rollback failed: $rollback_error\n"); } else { $class->_croak("Transaction aborted (rollback " . "successful): $error\n"); } } return(@return_values); } #eosub--do_transaction


Then, from my main program/script.
Then, I place this code in my program when I want to use a transaction. When I actually try to run it, I get a complaint referring to the last '$self' (},$self);.

my $user = MyClass::DBI->do_transaction(sub { my($self) = @_; my $model = MyClass::DBI::Users->create({ 'user_id' => $self->user_id, 'email_address' => $self->email_address, }); foreach my $group ($self->groups) { $model->add_to_groups({ 'group_id' => $group }); } return($model); },$self);


I thought I was doing so well, until I started to mess with adding do_transaction(). Now I'm ready to pull my hair out!! ARGH!

In reply to Class::DBI - Getting do_transaction() to work by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.