Revision 96670fe8
Von Moritz Bunkus vor etwa 8 Jahren hinzugefügt
SL/DB/Object.pm | ||
---|---|---|
140 | 140 |
sub save { |
141 | 141 |
my ($self, @args) = @_; |
142 | 142 |
|
143 |
my ($result, $exception); |
|
144 |
my $worker = sub { |
|
145 |
$exception = $EVAL_ERROR unless eval { |
|
146 |
SL::DB::Object::Hooks::run_hooks($self, 'before_save'); |
|
147 |
$result = $self->SUPER::save(@args); |
|
148 |
SL::DB::Object::Hooks::run_hooks($self, 'after_save', $result); |
|
149 |
1; |
|
150 |
}; |
|
143 |
my $result; |
|
151 | 144 |
|
152 |
return $result; |
|
153 |
}; |
|
145 |
$self->db->with_transaction(sub { |
|
146 |
SL::DB::Object::Hooks::run_hooks($self, 'before_save'); |
|
147 |
$result = $self->SUPER::save(@args); |
|
148 |
SL::DB::Object::Hooks::run_hooks($self, 'after_save', $result); |
|
154 | 149 |
|
155 |
$self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker); |
|
156 |
|
|
157 |
die $exception if $exception; |
|
150 |
1; |
|
151 |
}) || die $self->error; |
|
158 | 152 |
|
159 | 153 |
return $result; |
160 | 154 |
} |
... | ... | |
162 | 156 |
sub delete { |
163 | 157 |
my ($self, @args) = @_; |
164 | 158 |
|
165 |
my ($result, $exception); |
|
166 |
my $worker = sub { |
|
167 |
$exception = $EVAL_ERROR unless eval { |
|
168 |
SL::DB::Object::Hooks::run_hooks($self, 'before_delete'); |
|
169 |
$result = $self->SUPER::delete(@args); |
|
170 |
SL::DB::Object::Hooks::run_hooks($self, 'after_delete', $result); |
|
171 |
1; |
|
172 |
}; |
|
173 |
|
|
174 |
return $result; |
|
175 |
}; |
|
159 |
my $result; |
|
176 | 160 |
|
177 |
$self->db->in_transaction ? $worker->() : $self->db->do_transaction($worker); |
|
161 |
$self->db->with_transaction(sub { |
|
162 |
SL::DB::Object::Hooks::run_hooks($self, 'before_delete'); |
|
163 |
$result = $self->SUPER::delete(@args); |
|
164 |
SL::DB::Object::Hooks::run_hooks($self, 'after_delete', $result); |
|
178 | 165 |
|
179 |
die $exception if $exception; |
|
166 |
1; |
|
167 |
}) || die $self->error; |
|
180 | 168 |
|
181 | 169 |
return $result; |
182 | 170 |
} |
Auch abrufbar als: Unified diff
»with_transaction« anstelle von »do_transaction« verwenden
Es sollte so selten wie möglich »do_transaction« verwndet werden, damit
man sich immer angewöhnt, »with_transaction« zu nutzen.
Hintergründe und Unterschiede zwischen den beiden Funktionen sind in der
Dokumentation von SL/DB.pm beschrieben.