From a53f030194053afaee485771bcb0400740a0c7fd Mon Sep 17 00:00:00 2001 From: Espen Solli Grande Date: Sat, 28 Sep 2024 12:24:22 +0200 Subject: [PATCH 1/4] Add extra attributes pr duplicate for use in onCloned --- src/Cloneable.php | 14 ++++++++------ src/Cloner.php | 19 +++++++++++-------- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/Cloneable.php b/src/Cloneable.php index 018c9ee..2564c30 100644 --- a/src/Cloneable.php +++ b/src/Cloneable.php @@ -70,21 +70,22 @@ public function addCloneableRelation($relation) { /** * Clone the current model instance - * + * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new, saved clone */ - public function duplicate() { - return App::make('cloner')->duplicate($this); + public function duplicate($attr = null) { + return App::make('cloner')->duplicate($this, null, $attr); } /** * Clone the current model instance to a specific Laravel database connection * * @param string $connection A Laravel database connection + * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new, saved clone */ - public function duplicateTo($connection) { - return App::make('cloner')->duplicateTo($this, $connection); + public function duplicateTo($connection, $attr) { + return App::make('cloner')->duplicateTo($this, $connection, $attr); } /** @@ -93,9 +94,10 @@ public function duplicateTo($connection) { * * @param \Illuminate\Database\Eloquent\Model $src * @param boolean $child + * @param array $attr Extra attributes for each clone * @return void */ - public function onCloning($src, $child = null) {} + public function onCloning($src, $child = null, $attr = null) {} /** * A no-op callback that gets fired when a model is cloned and saved to the diff --git a/src/Cloner.php b/src/Cloner.php index 832e8f7..7030376 100644 --- a/src/Cloner.php +++ b/src/Cloner.php @@ -41,12 +41,13 @@ public function __construct(AttachmentAdapter $attachment = null, * * @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Relations\Relation $relation + * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new model instance */ - public function duplicate($model, $relation = null) { + public function duplicate($model, $relation = null, $attr = null) { $clone = $this->cloneModel($model); - $this->dispatchOnCloningEvent($clone, $relation, $model); + $this->dispatchOnCloningEvent($clone, $relation, $model,null, $attr); if ($relation) { if (!is_a($relation, 'Illuminate\Database\Eloquent\Relations\BelongsTo')) { @@ -71,11 +72,12 @@ public function duplicate($model, $relation = null) { * * @param \Illuminate\Database\Eloquent\Model $model * @param string $connection A Laravel database connection + * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new model instance */ - public function duplicateTo($model, $connection) { + public function duplicateTo($model, $connection, $attr) { $this->write_connection = $connection; // Store the write database connection - $clone = $this->duplicate($model); // Do a normal duplicate + $clone = $this->duplicate($model, null, $attr); // Do a normal duplicate $this->write_connection = null; // Null out the connection for next run return $clone; } @@ -114,17 +116,18 @@ protected function duplicateAttachments($model, $clone) { * @param \Illuminate\Database\Eloquent\Model $clone * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param \Illuminate\Database\Eloquent\Model $src The orginal model + * @param array $attr Extra attributes for each clone * @param boolean $child * @return void */ - protected function dispatchOnCloningEvent($clone, $relation = null, $src = null, $child = null) + protected function dispatchOnCloningEvent($clone, $relation = null, $src = null, $child = null, $attr = null) { // Set the child flag if ($relation) $child = true; - + if($attr) $attr = json_decode(json_encode($attr), FALSE); // Notify listeners via callback or event - if (method_exists($clone, 'onCloning')) $clone->onCloning($src, $child); - $this->events->dispatch('cloner::cloning: '.get_class($src), [$clone, $src]); + if (method_exists($clone, 'onCloning')) $clone->onCloning($src, $child, $attr); + $this->events->dispatch('cloner::cloning: '.get_class($src), [$clone, $src, $attr]); } /** From 7829c185c4089f2a11e79f47b8622db9db557eb0 Mon Sep 17 00:00:00 2001 From: Espen Solli Grande Date: Sat, 28 Sep 2024 12:33:50 +0200 Subject: [PATCH 2/4] tab clean up --- src/Cloneable.php | 2 +- src/Cloner.php | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Cloneable.php b/src/Cloneable.php index 2564c30..bbe15e1 100644 --- a/src/Cloneable.php +++ b/src/Cloneable.php @@ -81,7 +81,7 @@ public function duplicate($attr = null) { * Clone the current model instance to a specific Laravel database connection * * @param string $connection A Laravel database connection - * @param array $attr Extra attributes for each clone + * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new, saved clone */ public function duplicateTo($connection, $attr) { diff --git a/src/Cloner.php b/src/Cloner.php index 7030376..322f2fe 100644 --- a/src/Cloner.php +++ b/src/Cloner.php @@ -41,7 +41,7 @@ public function __construct(AttachmentAdapter $attachment = null, * * @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Relations\Relation $relation - * @param array $attr Extra attributes for each clone + * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new model instance */ public function duplicate($model, $relation = null, $attr = null) { @@ -61,9 +61,9 @@ public function duplicate($model, $relation = null, $attr = null) { $clone->save(); $this->cloneRelations($model, $clone); - + $this->dispatchOnClonedEvent($clone, $model); - + return $clone; } @@ -72,7 +72,7 @@ public function duplicate($model, $relation = null, $attr = null) { * * @param \Illuminate\Database\Eloquent\Model $model * @param string $connection A Laravel database connection - * @param array $attr Extra attributes for each clone + * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new model instance */ public function duplicateTo($model, $connection, $attr) { @@ -100,7 +100,7 @@ protected function cloneModel($model) { * Duplicate all attachments, given them a new name, and update the attribute * value * - * @param \Illuminate\Database\Eloquent\Model $model + * @param \Illuminate\Database\Eloquent\Model $model * @param \Illuminate\Database\Eloquent\Model $clone * @return void */ @@ -116,7 +116,7 @@ protected function duplicateAttachments($model, $clone) { * @param \Illuminate\Database\Eloquent\Model $clone * @param \Illuminate\Database\Eloquent\Relations\Relation $relation * @param \Illuminate\Database\Eloquent\Model $src The orginal model - * @param array $attr Extra attributes for each clone + * @param array $attr Extra attributes for each clone * @param boolean $child * @return void */ @@ -195,11 +195,11 @@ protected function duplicatePivotedRelation($relation, $relation_name, $clone) { $foreign->pivot->getCreatedAtColumn(), $foreign->pivot->getUpdatedAtColumn() ]); - + foreach (array_keys($pivot_attributes) as $attributeKey) { $pivot_attributes[$attributeKey] = $foreign->pivot->getAttribute($attributeKey); } - + if ($foreign->pivot->incrementing) { unset($pivot_attributes[$foreign->pivot->getKeyName()]); } From 705d7d2dc385ff4d52791f975845a60d37ad92fb Mon Sep 17 00:00:00 2001 From: Espen Solli Grande Date: Tue, 1 Oct 2024 09:25:20 +0200 Subject: [PATCH 3/4] Update src/Cloneable.php Co-authored-by: Robert Reinhard --- src/Cloneable.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cloneable.php b/src/Cloneable.php index bbe15e1..e465de9 100644 --- a/src/Cloneable.php +++ b/src/Cloneable.php @@ -84,7 +84,7 @@ public function duplicate($attr = null) { * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new, saved clone */ - public function duplicateTo($connection, $attr) { + public function duplicateTo($connection, $attr = null) { return App::make('cloner')->duplicateTo($this, $connection, $attr); } From 22a88b73bf2e61f37cc7359d9fce87a5046309a4 Mon Sep 17 00:00:00 2001 From: Espen Solli Grande Date: Tue, 1 Oct 2024 09:25:49 +0200 Subject: [PATCH 4/4] Update src/Cloner.php Co-authored-by: Robert Reinhard --- src/Cloner.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Cloner.php b/src/Cloner.php index 322f2fe..7d76101 100644 --- a/src/Cloner.php +++ b/src/Cloner.php @@ -75,7 +75,7 @@ public function duplicate($model, $relation = null, $attr = null) { * @param array $attr Extra attributes for each clone * @return \Illuminate\Database\Eloquent\Model The new model instance */ - public function duplicateTo($model, $connection, $attr) { + public function duplicateTo($model, $connection, $attr = null) { $this->write_connection = $connection; // Store the write database connection $clone = $this->duplicate($model, null, $attr); // Do a normal duplicate $this->write_connection = null; // Null out the connection for next run