From ae83d52898208b318f116755b033bb56cc898115 Mon Sep 17 00:00:00 2001 From: "G. Richardson" Date: Sun, 7 Aug 2016 22:46:56 +0200 Subject: [PATCH] =?utf8?q?Neue=20Methode=20clone=5Fand=5Freset=5Fdeep=20f?= =?utf8?q?=C3=BCr=20Part?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- SL/DB/Part.pm | 31 +++++++++++++++++++++++++++++++ SL/DB/Price.pm | 1 + 2 files changed, 32 insertions(+) diff --git a/SL/DB/Part.pm b/SL/DB/Part.pm index e095506f1..f95be9bfd 100644 --- a/SL/DB/Part.pm +++ b/SL/DB/Part.pm @@ -247,6 +247,37 @@ sub displayable_name { join ' ', grep $_, map $_[0]->$_, qw(partnumber description); } +sub clone_and_reset_deep { + my ($self) = @_; + + my $clone = $self->clone_and_reset; # resets id and partnumber (primary key and unique constraint) + $clone->makemodels( map { $_->clone_and_reset } @{$self->makemodels}); + $clone->translations( map { $_->clone_and_reset } @{$self->translations}); + + if ( $self->is_assortment ) { + $clone->assortment_items( map { $_->clone } @{$self->assortment_items} ); + foreach my $ai ( @{ $clone->assortment_items } ) { + $ai->assortment_id(undef); + }; + }; + + if ( $self->is_assembly ) { + $clone->assemblies( map { $_->clone_and_reset } @{$self->assemblies}); + }; + + if ( $self->prices ) { + $clone->prices( map { $_->clone } @{$self->prices}); # pricegroup_id gets reset here because it is part of a unique contraint + if ( $clone->prices ) { + foreach my $price ( @{$clone->prices} ) { + $price->id(undef); + $price->parts_id(undef); + }; + }; + }; + + return $clone; +} + 1; __END__ diff --git a/SL/DB/Price.pm b/SL/DB/Price.pm index 93f9ddec0..6c7e8af64 100644 --- a/SL/DB/Price.pm +++ b/SL/DB/Price.pm @@ -6,6 +6,7 @@ package SL::DB::Price; use strict; use SL::DB::MetaSetup::Price; +use Rose::DB::Object::Helpers qw(clone); __PACKAGE__->meta->initialize; -- 2.20.1