Skip to content

Commit

Permalink
Merge branch '3.x' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
octoberapp committed Sep 15, 2023
2 parents 79c1ba3 + 0aaeca1 commit 8a13773
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 10 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 @@ -63,6 +63,8 @@ public function __construct(array $attributes = [])

$this->extendableConstruct();

$this->initializeModelEvent();

$this->fill($attributes);
}

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

$this->extendableConstruct();

$this->initializeModelEvent();
}
}
15 changes: 15 additions & 0 deletions src/Database/Schema/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,19 @@
*/
class Blueprint extends BaseBlueprint
{
/**
* multisite adds columns used by the Multisite trait
*
* @param string $column
* @param string|null $indexName
* @return void
*/
public function multisite($column = 'site_id', $indexName = null)
{
$this->unsignedBigInteger($column)->nullable();

$this->unsignedBigInteger('site_root_id')->nullable();

$this->index([$column, 'site_root_id'], $indexName);
}
}
2 changes: 1 addition & 1 deletion src/Database/Traits/Multisite.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ protected function defineMultisiteRelation($name, $type = null)
if ($type === 'belongsToMany') {
$this->$type[$name]['parentKey'] = 'site_root_id';
}
elseif (in_array($type, ['hasOne', 'hasMany'])) {
elseif (in_array($type, ['belongsTo', 'hasOne', 'hasMany'])) {
$this->$type[$name]['otherKey'] = 'site_root_id';
}
elseif (in_array($type, ['attachOne', 'attachMany'])) {
Expand Down
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
14 changes: 9 additions & 5 deletions src/Database/Traits/Validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ protected function getRelationValidationValue($relationName)
return $value;
};

// Process singular
if ($this->isRelationTypeSingular($relationName)) {
if ($data instanceof \Illuminate\Support\Collection) {
$data = $data->last();
}

return $processValidationValue($data);
}

// Cast to primitive type
if ($data instanceof \Illuminate\Support\Collection) {
$data = $data->all();
Expand All @@ -202,11 +211,6 @@ protected function getRelationValidationValue($relationName)
return null;
}

// Process singular (last item)
if ($this->isRelationTypeSingular($relationName)) {
return $processValidationValue($data[array_key_last($data)]);
}

// Process multi
$result = [];

Expand Down

0 comments on commit 8a13773

Please sign in to comment.