Skip to content

Commit

Permalink
add replicating model event
Browse files Browse the repository at this point in the history
  • Loading branch information
jerguslejko committed Mar 31, 2019
1 parent a7e91c8 commit 7d24f91
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getObservableEvents()
return array_merge(
[
'retrieved', 'creating', 'created', 'updating', 'updated',
'saving', 'saved', 'restoring', 'restored',
'saving', 'saved', 'restoring', 'restored', 'replicating',
'deleting', 'deleted', 'forceDeleted',
],
$this->observables
Expand Down Expand Up @@ -303,6 +303,17 @@ public static function created($callback)
static::registerModelEvent('created', $callback);
}

/**
* Register a replicating model event with the dispatcher.
*
* @param \Closure|string $callback
* @return void
*/
public static function replicating($callback)
{
static::registerModelEvent('replicating', $callback);
}

/**
* Register a deleting model event with the dispatcher.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Database/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,8 @@ public function replicate(array $except = null)
$instance->setRawAttributes($attributes);

$instance->setRelations($this->relations);

$instance->fireModelEvent('replicating', false);
});
}

Expand Down
12 changes: 12 additions & 0 deletions tests/Database/DatabaseEloquentModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1507,6 +1507,18 @@ public function testReplicateCreatesANewModelInstanceWithSameAttributeValues()
$this->assertNull($replicated->updated_at);
}

public function testReplicatingEventIsFiredWhenReplicatingModel()
{
$model = new EloquentModelStub;

$model->setEventDispatcher($events = m::mock(Dispatcher::class));
$events->shouldReceive('dispatch')->once()->with('eloquent.replicating: '.get_class($model), m::on(function ($m) use ($model) {
return $model->is($m);
}));

$model->replicate();
}

public function testIncrementOnExistingModelCallsQueryAndSetsAttribute()
{
$model = m::mock(EloquentModelStub::class.'[newQueryWithoutRelationships]');
Expand Down

0 comments on commit 7d24f91

Please sign in to comment.