Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
4d312f8
Added SortableRelation trait
tobias-kuendig Oct 6, 2020
557b610
Added space after function keyword
tobias-kuendig Oct 6, 2020
2d1f3f4
add before/after save & create events
mjauvin Oct 27, 2020
da8579d
add before/after associate/dissociate events
mjauvin Oct 27, 2020
73ac6f0
Revert "add before/after save & create events"
mjauvin Oct 27, 2020
766ac61
do not make halting events
mjauvin Oct 28, 2020
8052d98
should not be halting events
mjauvin Oct 30, 2020
7f42aff
return parent results for overriden parent methods
mjauvin Oct 31, 2020
5762c7e
override associate/dissociate and fire before/after events there
mjauvin Nov 1, 2020
93176f3
do not pass true to Event::fire
mjauvin Nov 1, 2020
b33c08b
use a trait to avoid code duplication
mjauvin Nov 1, 2020
b59cf61
remove unused class
mjauvin Nov 1, 2020
5009e87
add example code in docblocks
mjauvin Nov 1, 2020
7e2b6e4
add parent::detach results to event params
mjauvin Nov 1, 2020
4b015d4
add {before,after}Add events to AttachOneOrMany
mjauvin Nov 3, 2020
4538d80
fix examples
mjauvin Nov 3, 2020
40215d0
fix typo
mjauvin Nov 3, 2020
24870fc
add remainging events
mjauvin Nov 6, 2020
5c73f26
call parent::attach() instead of duplicating code here
mjauvin Nov 8, 2020
36a6344
belongsto and morphto associate() methods have potentially different …
mjauvin Nov 8, 2020
6d333b9
not needed, code moved to each relations
mjauvin Nov 8, 2020
69019c0
properly pass the related model when firing the event
mjauvin Nov 8, 2020
70bff29
remove relatedModel event argument for dissociate()
mjauvin Nov 8, 2020
5142160
properly pass the related model when firing the event
mjauvin Nov 9, 2020
4601ae3
use $model method argument, not $this->model
mjauvin Nov 9, 2020
1812390
fix related model
mjauvin Nov 9, 2020
d0a040f
Merge branch 'develop' into add-relation-events
mjauvin Nov 21, 2020
1c62d59
no result returned from parent
mjauvin Nov 21, 2020
3d09007
fix typo
mjauvin Nov 21, 2020
74e1e3a
Merge branch 'develop' into add-relation-events
mjauvin Feb 7, 2021
51698b6
Update src/Database/Relations/BelongsToMany.php
mjauvin Feb 8, 2021
c746360
Merge branch 'develop' into more-relation-events
mjauvin Mar 27, 2021
12bcf51
Merge commit 'refs/pull/526/head' of https://github.com/octobercms/li…
mjauvin Mar 29, 2022
11af532
fix namespace
mjauvin Mar 29, 2022
cc47e2b
make it work for one to many relations as well
mjauvin Mar 31, 2022
911d08a
remove extra space
mjauvin Mar 31, 2022
84f018f
simplify code a bit; assign default order when undefined
mjauvin Apr 1, 2022
b3022d4
fix non pivot-based relation records initialization
mjauvin Apr 1, 2022
aaea530
use more appropriate model.beforeCreate event
mjauvin Apr 1, 2022
9107964
extend the class, not the instance
mjauvin Apr 1, 2022
aba5b17
rename to more correct "HasSortableRelations"
mjauvin Apr 1, 2022
8f587da
Merge branch 'wip/1.2' into sortable-relation-trait
mjauvin Apr 8, 2022
94993a0
make $sortableRelations visible in closure
mjauvin Apr 8, 2022
813cb08
use the new model.relation.afterAdd event
mjauvin Jun 4, 2022
580fef6
add model.relation.{before,after}Add events to morphOneOrMany relations
mjauvin Jun 4, 2022
bb16c93
Merge branch 'wip/1.2' into sortable-relation-trait
mjauvin Jun 4, 2022
12d1acb
fix code smell
mjauvin Jun 4, 2022
99e69b1
Merge https://github.com/wintercms/storm/pull/20
mjauvin Jun 4, 2022
c10a41f
Merge branch 'wip/1.2' into sortable-relation-trait
mjauvin Jun 7, 2022
11b7501
Merge branch 'wip/1.2' into sortable-relation-trait
mjauvin Jun 7, 2022
b95c7c9
Merge branch 'wip/1.2' into sortable-relation-trait
mjauvin Jun 20, 2022
b560793
Merge branch 'wip/1.2' into sortable-relation-trait
mjauvin Jun 25, 2022
992e399
Merge branch 'wip/1.2' into sortable-relation-trait
mjauvin Jul 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 84 additions & 0 deletions src/Database/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,90 @@ public function remove(Model $model, $sessionKey = null)
}
}

/**
* Associate the model instance to the given parent.
*
* @param \Illuminate\Database\Eloquent\Model|int|string $model
* @return \Illuminate\Database\Eloquent\Model
*/
public function associate($model)
{
/**
* @event model.relation.beforeAssociate
* Called before associating a relation to the model (only for BelongsTo/MorphTo relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeAssociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'dummyRelation') {
* throw new \Exception("Invalid relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $model]);

$result = parent::associate($model);

/**
* @event model.relation.afterAssociate
* Called after associating a relation to the model (only for BelongsTo/MorphTo relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterAssociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was associated as {$relationName} to {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $model]);

return $result;
}

/**
* Dissociate previously dissociated model from the given parent.
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function dissociate()
{
/**
* @event model.relation.beforeDissociate
* Called before dissociating a relation to the model (only for BelongsTo/MorphTo relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeDissociate', function (string $relationName) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'permanentRelation') {
* throw new \Exception("Cannot dissociate a permanent relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName]);

$result = parent::dissociate();

/**
* @event model.relation.afterDissociate
* Called after dissociating a relation to the model (only for BelongsTo/MorphTo relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterDissociate', function (string $relationName) use (\October\Rain\Database\Model $model) {
* $modelClass = get_class($model);
* traceLog("{$relationName} was dissociated from {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName]);

return $result;
}

/**
* Helper for setting this relationship using various expected
* values. For example, $model->relation = $value;
Expand Down
60 changes: 60 additions & 0 deletions src/Database/Relations/Concerns/AttachOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,21 @@ public function add(Model $model, $sessionKey = null)
}

if ($sessionKey === null) {
/**
* @event model.relation.beforeAdd
* Called before adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'dummyRelation') {
* throw new \Exception("Invalid relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $model]);

// Delete siblings for single attachments
if ($this instanceof AttachOne) {
$this->delete();
Expand All @@ -186,6 +201,21 @@ public function add(Model $model, $sessionKey = null)
else {
$this->parent->reloadRelations($this->relationName);
}

/**
* @event model.relation.afterAdd
* Called after adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was added as {$relationName} to {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]);
}
else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
Expand All @@ -210,6 +240,21 @@ public function addMany($models, $sessionKey = null)
public function remove(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
/**
* @event model.relation.beforeRemove
* Called before removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeRemove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'permanentRelation') {
* throw new \Exception("Cannot dissociate a permanent relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $model]);

$options = $this->parent->getRelationDefinition($this->relationName);

if (array_get($options, 'delete', false)) {
Expand All @@ -234,6 +279,21 @@ public function remove(Model $model, $sessionKey = null)
else {
$this->parent->reloadRelations($this->relationName);
}

/**
* @event model.relation.afterRemove
* Called after removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterRemove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was removed from {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $model]);
}
else {
$this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
Expand Down
59 changes: 59 additions & 0 deletions src/Database/Relations/Concerns/HasOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ public function create(array $attributes = [], $sessionKey = null)
public function add(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
/**
* @event model.relation.beforeAdd
* Called before adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'dummyRelation') {
* throw new \Exception("Invalid relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $model]);

$model->setAttribute($this->getForeignKeyName(), $this->getParentKey());

if (!$model->exists || $model->isDirty()) {
Expand All @@ -72,6 +87,21 @@ public function add(Model $model, $sessionKey = null)
else {
$this->parent->reloadRelations($this->relationName);
}

/**
* @event model.relation.afterAdd
* Called after adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was added as {$relationName} to {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]);
}
else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
Expand All @@ -96,6 +126,21 @@ public function addMany($models, $sessionKey = null)
public function remove(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
/**
* @event model.relation.beforeRemove
* Called before removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeRemove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'permanentRelation') {
* throw new \Exception("Cannot dissociate a permanent relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $model]);

$model->setAttribute($this->getForeignKeyName(), null);
$model->save();

Expand All @@ -108,6 +153,20 @@ public function remove(Model $model, $sessionKey = null)
else {
$this->parent->reloadRelations($this->relationName);
}
/**
* @event model.relation.afterRemove
* Called after removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterRemove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was removed from {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $model]);
}
else {
$this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
Expand Down
60 changes: 60 additions & 0 deletions src/Database/Relations/Concerns/MorphOneOrMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,21 @@ public function create(array $attributes = [], $sessionKey = null)
public function add(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
/**
* @event model.relation.beforeAdd
* Called before adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'dummyRelation') {
* throw new \Exception("Invalid relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $model]);

$model->setAttribute($this->getForeignKeyName(), $this->getParentKey());
$model->setAttribute($this->getMorphType(), $this->morphClass);
$model->save();
Expand All @@ -58,6 +73,21 @@ public function add(Model $model, $sessionKey = null)
else {
$this->parent->reloadRelations($this->relationName);
}

/**
* @event model.relation.afterAdd
* Called after adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was added as {$relationName} to {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]);
}
else {
$this->parent->bindDeferred($this->relationName, $model, $sessionKey);
Expand All @@ -70,6 +100,21 @@ public function add(Model $model, $sessionKey = null)
public function remove(Model $model, $sessionKey = null)
{
if ($sessionKey === null) {
/**
* @event model.relation.beforeRemove
* Called before removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.beforeRemove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* if ($relationName === 'permanentRelation') {
* throw new \Exception("Cannot dissociate a permanent relation!");
* }
* });
*
*/
$this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $model]);

$options = $this->parent->getRelationDefinition($this->relationName);

if (array_get($options, 'delete', false)) {
Expand All @@ -93,6 +138,21 @@ public function remove(Model $model, $sessionKey = null)
else {
$this->parent->reloadRelations($this->relationName);
}

/**
* @event model.relation.afterRemove
* Called after removing a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations)
*
* Example usage:
*
* $model->bindEvent('model.relation.afterRemove', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) {
* $relatedClass = get_class($relatedModel);
* $modelClass = get_class($model);
* traceLog("{$relatedClass} was removed from {$modelClass}.");
* });
*
*/
$this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $model]);
}
else {
$this->parent->unbindDeferred($this->relationName, $model, $sessionKey);
Expand Down
Loading