Subject: | Cookbook doc patch |
Might be helpful in "Automatically creating related objects" to have an example
w/transaction, since it requires $self->next::can . E.g. add
If you want to wrap the inserts in a transaction, you'll need to get a
ref to the overloaded method and pass that along:
sub insert {
my ( $self, @args ) = @_;
my $artist_insert = $self->next::can;
my $insert_artist_and_cds = sub {
$artist_insert->($self, @args);
$self->cds->new({})->fill_from_artist($self)->insert;
}
eval {
$self->result_source->schema->txn_do ($insert_artist_and_cds);
};
if ($@) {
deal_with_failed_transaction();
}
return $self;
}
Subject: | cookbook.patch |
--- Cookbook.pod 2009-12-11 13:01:53.000000000 -0500
+++ Cookbook.pod.new 2009-12-11 13:13:03.000000000 -0500
@@ -1827,6 +1827,30 @@
where C<fill_from_artist> is a method you specify in C<CD> which sets
values in C<CD> based on the data in the C<Artist> object you pass in.
+If you want to wrap the inserts in a transaction, you'll need to get a
+ref to the overloaded method and pass that along:
+
+ sub insert {
+ my ( $self, @args ) = @_;
+
+ my $artist_insert = $self->next::can;
+
+ my $insert_artist_and_cds = sub {
+ $artist_insert->($self, @args);
+ $self->cds->new({})->fill_from_artist($self)->insert;
+ }
+
+ eval {
+ $self->result_source->schema->txn_do ($insert_artist_and_cds);
+ };
+ if ($@) {
+ deal_with_failed_transaction();
+ }
+
+ return $self;
+ }
+
+
=head2 Wrapping/overloading a column accessor
B<Problem:>