Skip to content

Commit

Permalink
Merge pull request #63 from espensgr/develop
Browse files Browse the repository at this point in the history
Add extra attributes pr duplicate for use in onCloned
  • Loading branch information
weotch authored Oct 1, 2024
2 parents 6c2da68 + 22a88b7 commit 3cbb48b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 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 = null) {
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
29 changes: 16 additions & 13 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 @@ -60,9 +61,9 @@ public function duplicate($model, $relation = null) {
$clone->save();

$this->cloneRelations($model, $clone);

$this->dispatchOnClonedEvent($clone, $model);

return $clone;
}

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 = null) {
$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 All @@ -98,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
*/
Expand All @@ -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 Expand Up @@ -192,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()]);
}
Expand Down

0 comments on commit 3cbb48b

Please sign in to comment.