Skip to content

Commit

Permalink
Add extra attributes pr duplicate for use in onCloned
Browse files Browse the repository at this point in the history
  • Loading branch information
espensgr committed Sep 28, 2024
1 parent 6c2da68 commit a53f030
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
14 changes: 8 additions & 6 deletions src/Cloneable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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
Expand Down
19 changes: 11 additions & 8 deletions src/Cloner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -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;
}
Expand Down Expand Up @@ -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]);
}

/**
Expand Down

0 comments on commit a53f030

Please sign in to comment.