Skip to content

Commit

Permalink
Adds an init event, needed for extensibility
Browse files Browse the repository at this point in the history
1. Model constructed
2. Model ::extend called
3. Model initializes
  • Loading branch information
octoberapp committed Sep 15, 2023
1 parent c5bec9b commit 0aaeca1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
31 changes: 29 additions & 2 deletions src/Database/Concerns/HasEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ protected function bootNicerEvents()
static::$eventsBooted[$class] = true;
}

/**
* initializeModelEvent is called every time the model is constructed.
*/
protected function initializeModelEvent()
{
$this->fireEvent('model.afterInit');
$this->afterInit();
}

/**
* flushEventListeners removes all of the event listeners for the model
* Also flush registry of models that had events booted
Expand Down Expand Up @@ -104,8 +113,7 @@ public static function fetched($callback)
}

/**
* afterBoot is called after the model is constructed, a nicer version
* of overriding the __construct method.
* afterBoot is called after the model is constructed for the first time.
*/
protected function afterBoot()
{
Expand All @@ -122,6 +130,25 @@ protected function afterBoot()
*/
}

/**
* afterInit is called after the model is constructed, a nicer version
* of overriding the __construct method.
*/
protected function afterInit()
{
/**
* @event model.afterInit
* Called after the model is initialized
*
* Example usage:
*
* $model->bindEvent('model.afterInit', function () use (\October\Rain\Database\Model $model) {
* \Log::info(get_class($model) . ' has initialized');
* });
*
*/
}

/**
* beforeCreate handles the "creating" model event
*/
Expand Down
4 changes: 4 additions & 0 deletions src/Database/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function __construct(array $attributes = [])

$this->extendableConstruct();

$this->initializeModelEvent();

$this->fill($attributes);
}

Expand Down Expand Up @@ -530,5 +532,7 @@ public function __wakeup()
$this->bootNicerEvents();

$this->extendableConstruct();

$this->initializeModelEvent();
}
}
6 changes: 4 additions & 2 deletions src/Database/Traits/SortableRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ trait SortableRelation
*/
public function initializeSortableRelation()
{
$this->bindEvent('model.afterInit', function() {
$this->defineSortableRelations();
});

$this->bindEvent('model.relation.attach', function ($relationName, $attached, $data) {
if (!array_key_exists($relationName, $this->getSortableRelations())) {
return;
Expand All @@ -45,8 +49,6 @@ public function initializeSortableRelation()
$this->$relationName()->updateExistingPivot($id, [$column => ++$order]);
}
});

$this->defineSortableRelations();
}

/**
Expand Down

0 comments on commit 0aaeca1

Please sign in to comment.