Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision da6a187a

Von Moritz Bunkus vor mehr als 8 Jahren hinzugefügt

  • ID da6a187ab3fc3cbb97e094559dc964a9809f347b
  • Vorgänger 86bc3cfa
  • Nachfolger 948ad06a

SL::DB::Object: clone_and_reset unter Umgehung von itime, mtime

Unterschiede anzeigen:

SL/DB/CsvImportProfile.pm
5 5
use List::Util qw(first);
6 6

  
7 7
require SL::DB::MetaSetup::CsvImportProfile;
8
use Rose::DB::Object::Helpers qw(clone_and_reset);
9 8

  
10 9
__PACKAGE__->meta->add_relationship(
11 10
  settings => {
SL/DB/DeliveryOrder.pm
101 101
sub _clone_orderitem_cvar {
102 102
  my ($cvar) = @_;
103 103

  
104
  my $cloned = Rose::DB::Object::Helpers::clone_and_reset($_);
104
  my $cloned = $_->clone_and_reset;
105 105
  $cloned->sub_module('delivery_order_items');
106 106

  
107 107
  return $cloned;
SL/DB/Invoice.pm
131 131
sub _clone_orderitem_delivery_order_item_cvar {
132 132
  my ($cvar) = @_;
133 133

  
134
  my $cloned = Rose::DB::Object::Helpers::clone_and_reset($_);
134
  my $cloned = $_->clone_and_reset;
135 135
  $cloned->sub_module('invoice');
136 136

  
137 137
  return $cloned;
SL/DB/Object.pm
5 5
use Carp;
6 6
use English qw(-no_match_vars);
7 7
use Rose::DB::Object;
8
use List::MoreUtils qw(any);
8
use Rose::DB::Object::Constants qw();
9
use List::MoreUtils qw(any pairwise);
9 10

  
10 11
use SL::DB;
11 12
use SL::DB::Helper::Attr;
......
218 219
  return $class_or_self;
219 220
}
220 221

  
222
my %_skip_fields_when_cloning = map { ($_ => 1) } qw(itime mtime);
223

  
224
sub clone_and_reset {
225
  my($self)               = shift;
226
  my $class               = ref $self;
227
  my $cloning             = Rose::DB::Object::Constants::STATE_CLONING();
228
  local $self->{$cloning} = 1;
229

  
230
  my $meta                = $class->meta;
231
  my @accessors           = $meta->column_accessor_method_names;
232
  my @mutators            = $meta->column_mutator_method_names;
233
  my @column_names        =
234
    grep     { $_->[0] && $_->[1] && !$_skip_fields_when_cloning{ $_->[0] } }
235
    pairwise { [ $a, $b] } @accessors, @mutators;
236

  
237
  my $clone = $class->new(map { my $method = $_->[0]; ($_->[1] => $self->$method) } @column_names);
238

  
239
  # Blank all primary and unique key columns
240
  my @keys = (
241
    $meta->primary_key_column_mutator_names,
242
    map { my $uk = $_; map { $meta->column_mutator_method_name($_) } ($uk->columns) } ($meta->unique_keys)
243
  );
244

  
245
  $clone->$_(undef) for @keys;
246

  
247
  # Also copy db object, if any
248
  $clone->db($self->{db}) if $self->{db};
249

  
250
  return $clone;
251
}
252

  
221 253
1;
222 254

  
223 255
__END__
......
323 355

  
324 356
Returns the object/class it was called on.
325 357

  
358
=item C<clone_and_reset>
359

  
360
This works similar to L<Rose::DB::Object::Helpers/clone_and_reset>: it
361
returns a cloned instance of C<$self>. All primary and unique key
362
fields have been reset.
363

  
364
The difference between Rose's and this function is that this function
365
will also skip setting the following fields if such columns exist for
366
C<$self>: C<itime>, C<mtime>.
367

  
326 368
=back
327 369

  
328 370
=head1 AUTHOR
SL/DB/PriceRule.pm
7 7

  
8 8
use SL::DB::MetaSetup::PriceRule;
9 9
use SL::DB::Manager::PriceRule;
10
use Rose::DB::Object::Helpers qw(clone_and_reset);
11 10
use SL::Locale::String qw(t8);
12 11

  
13 12
__PACKAGE__->meta->add_relationship(
SL/DB/PriceRuleItem.pm
7 7

  
8 8
use SL::DB::MetaSetup::PriceRuleItem;
9 9
use SL::DB::Manager::PriceRuleItem;
10
use Rose::DB::Object::Helpers qw(clone_and_reset);
11 10
use SL::Locale::String qw(t8);
12 11

  
13 12
__PACKAGE__->meta->initialize;
SL/DB/RequirementSpec.pm
156 156
sub _create_copy {
157 157
  my ($self, %params) = @_;
158 158

  
159
  my $copy = Rose::DB::Object::Helpers::clone_and_reset($self);
159
  my $copy = $self->clone_and_reset;
160 160
  $copy->copy_from($self, %params);
161 161

  
162 162
  return $copy;
......
185 185
  # Clone text blocks and pictures.
186 186
  my $clone_and_reset_position = sub {
187 187
    my ($src_obj) = @_;
188
    my $cloned    = Rose::DB::Object::Helpers::clone_and_reset($src_obj);
188
    my $cloned    = $src_obj->clone_and_reset;
189 189
    $cloned->position(undef);
190 190
    return $cloned;
191 191
  };
192 192

  
193 193
  my $clone_text_block = sub {
194 194
    my ($text_block) = @_;
195
    my $cloned       = Rose::DB::Object::Helpers::clone_and_reset($text_block);
195
    my $cloned       = $text_block->clone_and_reset;
196 196
    $cloned->position(undef);
197 197
    $cloned->pictures([ map { $clone_and_reset_position->($_) } @{ $text_block->pictures_sorted } ]);
198 198
    return $cloned;
......
220 220
  my $clone_item;
221 221
  $clone_item = sub {
222 222
    my ($item) = @_;
223
    my $cloned = Rose::DB::Object::Helpers::clone_and_reset($item);
223
    my $cloned = $item->clone_and_reset;
224 224
    $cloned->requirement_spec_id($self->id);
225 225
    $cloned->position(undef);
226 226
    $cloned->fb_number(undef) if $params->{paste_template};

Auch abrufbar als: Unified diff