From 2d1f3f430c231181ba8907a09071cffab8ff4b60 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 27 Oct 2020 10:34:02 -0400 Subject: [PATCH 01/27] add before/after save & create events --- src/Database/Relations/HasOneOrMany.php | 60 ++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/src/Database/Relations/HasOneOrMany.php b/src/Database/Relations/HasOneOrMany.php index 7d2325257..d06ed5ade 100644 --- a/src/Database/Relations/HasOneOrMany.php +++ b/src/Database/Relations/HasOneOrMany.php @@ -16,12 +16,42 @@ trait HasOneOrMany */ public function save(Model $model, $sessionKey = null) { + /** + * @event model.relation.beforeSave + * Called before saving a relation (only for HasOneOrMany relation) + * + * Example usage: + * + * $model->bindEvent('model.relation.beforeSave', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + if ($this->parent->fireEvent('model.relation.beforeSave', [$this->relationName, $this->related], true) === false) { + return; + } + if ($sessionKey === null) { return parent::save($model); } $this->add($model, $sessionKey); - return $model->save() ? $model : false; + $result = $model->save() ? $model : false; + + /** + * @event model.relation.afterSave + * Called after saving a relation (only for HasOneOrMany relation) + * + * Example usage: + * + * $model->bindEvent('model.relation.afterSave', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterSave', [$this->relationName, $this->related]); + + return $result; } /** @@ -41,12 +71,40 @@ public function saveMany($models, $sessionKey = null) */ public function create(array $attributes = [], $sessionKey = null) { + /** + * @event model.relation.beforeCreate + * Called before creating a new relation between models (only for HasOneOrMany relation) + * + * Example usage: + * + * $model->bindEvent('model.relation.beforeCreate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + if ($this->parent->fireEvent('model.relation.beforeCreate', [$this->relationName, $this->related], true) === false) { + return; + } + $model = parent::create($attributes); if ($sessionKey !== null) { $this->add($model, $sessionKey); } + /** + * @event model.relation.afterCreate + * Called after creating a new relation between models (only for HasOneOrMany relation) + * + * Example usage: + * + * $model->bindEvent('model.relation.afterCreate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterCreate', [$this->relationName, $this->related]); + return $model; } From da8579d4e0b57abca790188b036eda5eff50e3a8 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 27 Oct 2020 14:46:58 -0400 Subject: [PATCH 02/27] add before/after associate/dissociate events --- src/Database/Relations/BelongsTo.php | 56 +++++++++++++++++++ src/Database/Relations/MorphTo.php | 81 ++++++++++++++++++++++++++++ 2 files changed, 137 insertions(+) diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index afd3403a5..7be22acf2 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -28,12 +28,40 @@ public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey */ public function add(Model $model, $sessionKey = null) { + /** + * @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) { + * TODO: add example code + * }); + * + */ + if ($this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related], true) === false) { + return; + } + if ($sessionKey === null) { $this->associate($model); } else { $this->child->bindDeferred($this->relationName, $model, $sessionKey); } + + /** + * @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) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); } /** @@ -41,12 +69,40 @@ public function add(Model $model, $sessionKey = null) */ public function remove(Model $model, $sessionKey = null) { + /** + * @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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + if ($this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related], true) === false) { + return; + } + if ($sessionKey === null) { $this->dissociate(); } else { $this->child->unbindDeferred($this->relationName, $model, $sessionKey); } + + /** + * @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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); } /** diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index 39975e0c8..a882cf212 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -70,4 +70,85 @@ public function getSimpleValue() $this->parent->getAttribute($this->morphType) ]; } + + /** + * Associate the model instance to the given parent. + * + * @param \Illuminate\Database\Eloquent\Model $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) { + * TODO: add example code + * }); + * + */ + if ($this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related], true) === false) { + return; + } + + $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) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); + + 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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + if ($this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related], true) === false) { + return; + } + + $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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); + + return $result; + } } From 73ac6f0ea4ed7f9d4e337bb6687dcd124e864b5d Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 27 Oct 2020 15:28:30 -0400 Subject: [PATCH 03/27] Revert "add before/after save & create events" This reverts commit 2d1f3f430c231181ba8907a09071cffab8ff4b60. --- src/Database/Relations/HasOneOrMany.php | 60 +------------------------ 1 file changed, 1 insertion(+), 59 deletions(-) diff --git a/src/Database/Relations/HasOneOrMany.php b/src/Database/Relations/HasOneOrMany.php index d06ed5ade..7d2325257 100644 --- a/src/Database/Relations/HasOneOrMany.php +++ b/src/Database/Relations/HasOneOrMany.php @@ -16,42 +16,12 @@ trait HasOneOrMany */ public function save(Model $model, $sessionKey = null) { - /** - * @event model.relation.beforeSave - * Called before saving a relation (only for HasOneOrMany relation) - * - * Example usage: - * - * $model->bindEvent('model.relation.beforeSave', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - if ($this->parent->fireEvent('model.relation.beforeSave', [$this->relationName, $this->related], true) === false) { - return; - } - if ($sessionKey === null) { return parent::save($model); } $this->add($model, $sessionKey); - $result = $model->save() ? $model : false; - - /** - * @event model.relation.afterSave - * Called after saving a relation (only for HasOneOrMany relation) - * - * Example usage: - * - * $model->bindEvent('model.relation.afterSave', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.afterSave', [$this->relationName, $this->related]); - - return $result; + return $model->save() ? $model : false; } /** @@ -71,40 +41,12 @@ public function saveMany($models, $sessionKey = null) */ public function create(array $attributes = [], $sessionKey = null) { - /** - * @event model.relation.beforeCreate - * Called before creating a new relation between models (only for HasOneOrMany relation) - * - * Example usage: - * - * $model->bindEvent('model.relation.beforeCreate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - if ($this->parent->fireEvent('model.relation.beforeCreate', [$this->relationName, $this->related], true) === false) { - return; - } - $model = parent::create($attributes); if ($sessionKey !== null) { $this->add($model, $sessionKey); } - /** - * @event model.relation.afterCreate - * Called after creating a new relation between models (only for HasOneOrMany relation) - * - * Example usage: - * - * $model->bindEvent('model.relation.afterCreate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.afterCreate', [$this->relationName, $this->related]); - return $model; } From 766ac611d068f50069a4e3033992a037142e80c6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 27 Oct 2020 22:01:35 -0400 Subject: [PATCH 04/27] do not make halting events --- src/Database/Relations/BelongsTo.php | 8 ++------ src/Database/Relations/MorphTo.php | 8 ++------ 2 files changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index 7be22acf2..89d774a3f 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -39,9 +39,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - if ($this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related], true) === false) { - return; - } + $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); if ($sessionKey === null) { $this->associate($model); @@ -80,9 +78,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - if ($this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related], true) === false) { - return; - } + $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related], true); if ($sessionKey === null) { $this->dissociate(); diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index a882cf212..1ca0b955a 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -90,9 +90,7 @@ public function associate($model) * }); * */ - if ($this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related], true) === false) { - return; - } + $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related], true); $result = parent::associate($model); @@ -130,9 +128,7 @@ public function dissociate() * }); * */ - if ($this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related], true) === false) { - return; - } + $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related]); $result = parent::dissociate(); From 8052d989aedbd0a437a5d361a0151abf0edfffd2 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 30 Oct 2020 08:01:21 -0400 Subject: [PATCH 05/27] should not be halting events --- src/Database/Relations/BelongsToMany.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index 0275ff3e2..cd818e97d 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -126,9 +126,7 @@ public function attach($id, array $attributes = [], $touch = true) * }); * */ - if ($this->parent->fireEvent('model.relation.beforeAttach', [$this->relationName, $attachedIdList, $insertData], true) === false) { - return; - } + $this->parent->fireEvent('model.relation.beforeAttach', [$this->relationName, $attachedIdList, $insertData]); // Here we will insert the attachment records into the pivot table. Once we have // inserted the records, we will touch the relationships if necessary and the @@ -181,9 +179,7 @@ public function detach($ids = null, $touch = true) * }); * */ - if ($this->parent->fireEvent('model.relation.beforeDetach', [$this->relationName, $attachedIdList], true) === false) { - return; - } + $this->parent->fireEvent('model.relation.beforeDetach', [$this->relationName, $attachedIdList]); /* * See Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable From 7f42aff81442e532e27cf0074ffa57a23721c593 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 31 Oct 2020 00:15:28 -0400 Subject: [PATCH 06/27] return parent results for overriden parent methods --- src/Database/Relations/BelongsToMany.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index cd818e97d..fb6ba0330 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -103,9 +103,11 @@ public function create(array $attributes = [], array $pivotData = [], $sessionKe /** * Override attach() method of BelongToMany relation. * This is necessary in order to fire 'model.relation.beforeAttach', 'model.relation.afterAttach' events + * * @param mixed $id * @param array $attributes * @param bool $touch + * @return void */ public function attach($id, array $attributes = [], $touch = true) { @@ -154,9 +156,10 @@ public function attach($id, array $attributes = [], $touch = true) /** * Override detach() method of BelongToMany relation. * This is necessary in order to fire 'model.relation.beforeDetach', 'model.relation.afterDetach' events + * * @param null $ids * @param bool $touch - * @return int|void + * @return int */ public function detach($ids = null, $touch = true) { @@ -184,7 +187,7 @@ public function detach($ids = null, $touch = true) /* * See Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable */ - parent::detach($ids, $touch); + $result = parent::detach($ids, $touch); /** * @event model.relation.afterDetach @@ -198,6 +201,8 @@ public function detach($ids = null, $touch = true) * */ $this->parent->fireEvent('model.relation.afterDetach', [$this->relationName, $attachedIdList]); + + return $result; } /** From 5762c7e75d6b5df81a74f668e89608c5f7ab65a4 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 1 Nov 2020 08:58:34 -0500 Subject: [PATCH 07/27] override associate/dissociate and fire before/after events there --- src/Database/Relations/BelongsTo.php | 51 +++++++++++++++++++++------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index 89d774a3f..5acf33d24 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -27,6 +27,22 @@ public function __construct(Builder $query, Model $child, $foreignKey, $ownerKey * Adds a model to this relationship type. */ public function add(Model $model, $sessionKey = null) + { + if ($sessionKey === null) { + $this->associate($model); + } + else { + $this->child->bindDeferred($this->relationName, $model, $sessionKey); + } + } + + /** + * 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 @@ -41,12 +57,7 @@ public function add(Model $model, $sessionKey = null) */ $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); - if ($sessionKey === null) { - $this->associate($model); - } - else { - $this->child->bindDeferred($this->relationName, $model, $sessionKey); - } + $result = parent::associate($model); /** * @event model.relation.afterAssociate @@ -60,12 +71,29 @@ public function add(Model $model, $sessionKey = null) * */ $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); + + return $result; } /** * Removes a model from this relationship type. */ public function remove(Model $model, $sessionKey = null) + { + if ($sessionKey === null) { + $this->dissociate(); + } + else { + $this->child->unbindDeferred($this->relationName, $model, $sessionKey); + } + } + + /** + * Dissociate previously associated model from the given parent. + * + * @return \Illuminate\Database\Eloquent\Model + */ + public function dissociate() { /** * @event model.relation.beforeDissociate @@ -78,14 +106,9 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related], true); + $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related]); - if ($sessionKey === null) { - $this->dissociate(); - } - else { - $this->child->unbindDeferred($this->relationName, $model, $sessionKey); - } + $result = parent::dissociate(); /** * @event model.relation.afterDissociate @@ -99,6 +122,8 @@ public function remove(Model $model, $sessionKey = null) * */ $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); + + return $result; } /** From 93176f3fa1f9c891e5800c8948261be0f42250df Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 1 Nov 2020 09:01:38 -0500 Subject: [PATCH 08/27] do not pass true to Event::fire --- src/Database/Relations/MorphTo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index 1ca0b955a..1060913a5 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -90,7 +90,7 @@ public function associate($model) * }); * */ - $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related], true); + $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); $result = parent::associate($model); From b33c08b6ac5e36d046758a953139e37953707b2d Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 1 Nov 2020 11:12:13 -0500 Subject: [PATCH 09/27] use a trait to avoid code duplication --- src/Database/Relations/BelongsOrMorphTo.php | 83 +++++++++++++++++++++ src/Database/Relations/BelongsTo.php | 78 +------------------ src/Database/Relations/MorphTo.php | 78 +------------------ 3 files changed, 85 insertions(+), 154 deletions(-) create mode 100644 src/Database/Relations/BelongsOrMorphTo.php diff --git a/src/Database/Relations/BelongsOrMorphTo.php b/src/Database/Relations/BelongsOrMorphTo.php new file mode 100644 index 000000000..e1891d77a --- /dev/null +++ b/src/Database/Relations/BelongsOrMorphTo.php @@ -0,0 +1,83 @@ +bindEvent('model.relation.beforeAssociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); + + $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) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); + + 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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related]); + + $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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * TODO: add example code + * }); + * + */ + $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); + + return $result; + } +} diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index 5acf33d24..da5373b38 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -8,6 +8,7 @@ class BelongsTo extends BelongsToBase { use DeferOneOrMany; use DefinedConstraints; + use BelongsOrMorphTo; /** * @var string The "name" of the relationship. @@ -36,45 +37,6 @@ public function add(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) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); - - $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) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); - - return $result; - } - /** * Removes a model from this relationship type. */ @@ -88,44 +50,6 @@ public function remove(Model $model, $sessionKey = null) } } - /** - * Dissociate previously associated 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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related]); - - $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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); - - return $result; - } - /** * Helper for setting this relationship using various expected * values. For example, $model->relation = $value; diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index 1060913a5..dd7ec8ec0 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -7,6 +7,7 @@ class MorphTo extends MorphToBase { use DefinedConstraints; + use BelongsOrMorphTo; /** * @var string The "name" of the relationship. @@ -70,81 +71,4 @@ public function getSimpleValue() $this->parent->getAttribute($this->morphType) ]; } - - /** - * Associate the model instance to the given parent. - * - * @param \Illuminate\Database\Eloquent\Model $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) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); - - $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) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); - - 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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName, $this->related]); - - $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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code - * }); - * - */ - $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); - - return $result; - } } From b59cf6156e045a7936485f436b434df88107cec9 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 1 Nov 2020 11:15:08 -0500 Subject: [PATCH 10/27] remove unused class --- src/Database/Relations/BelongsOrMorphTo.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Database/Relations/BelongsOrMorphTo.php b/src/Database/Relations/BelongsOrMorphTo.php index e1891d77a..ed9c3483c 100644 --- a/src/Database/Relations/BelongsOrMorphTo.php +++ b/src/Database/Relations/BelongsOrMorphTo.php @@ -1,7 +1,5 @@ Date: Sun, 1 Nov 2020 11:31:41 -0500 Subject: [PATCH 11/27] add example code in docblocks --- src/Database/Relations/BelongsOrMorphTo.php | 14 ++++++++++---- src/Database/Relations/BelongsToMany.php | 1 - 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/Database/Relations/BelongsOrMorphTo.php b/src/Database/Relations/BelongsOrMorphTo.php index ed9c3483c..d5ddde8c5 100644 --- a/src/Database/Relations/BelongsOrMorphTo.php +++ b/src/Database/Relations/BelongsOrMorphTo.php @@ -17,7 +17,9 @@ public function associate($model) * Example usage: * * $model->bindEvent('model.relation.beforeAssociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code + * if ($relationName === 'dummyRelation') { + * throw new \Exception("Invalid relation!"); + * } * }); * */ @@ -32,7 +34,8 @@ public function associate($model) * Example usage: * * $model->bindEvent('model.relation.afterAssociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code + * $relatedClass = get_class($relatedModel); + * traceLog("Relation {$relationName} was associated to model {$relatedClass}."); * }); * */ @@ -55,7 +58,9 @@ public function dissociate() * Example usage: * * $model->bindEvent('model.relation.beforeDissociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code + * if ($relationName === 'permanentRelation') { + * throw new \Exception("Cannot dissociate a permanent relation!"); + * } * }); * */ @@ -70,7 +75,8 @@ public function dissociate() * Example usage: * * $model->bindEvent('model.relation.afterDissociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * TODO: add example code + * $relatedClass = get_class($relatedModel); + * traceLog("Relation {$relationName} was dissociated from model {$relatedClass}."); * }); * */ diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index fb6ba0330..744a3ea7a 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -123,7 +123,6 @@ public function attach($id, array $attributes = [], $touch = true) * $model->bindEvent('model.relation.beforeAttach', function (string $relationName, array $attachedIdList, array $insertData) use (\October\Rain\Database\Model $model) { * if (!$model->isRelationValid($attachedIdList)) { * throw new \Exception("Invalid relation!"); - * return false; * } * }); * From 7e2b6e4ca0d0edb0f527be4cc245aa94b572cfc6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 1 Nov 2020 17:34:42 -0500 Subject: [PATCH 12/27] add parent::detach results to event params --- src/Database/Relations/BelongsToMany.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index 744a3ea7a..d76cd9281 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -194,12 +194,12 @@ public function detach($ids = null, $touch = true) * * Example usage: * - * $model->bindEvent('model.relation.afterDetach', function (string $relationName, array $attachedIdList) use (\October\Rain\Database\Model $model) { - * traceLog("Relation {$relationName} was removed", $attachedIdList); + * $model->bindEvent('model.relation.afterDetach', function (string $relationName, array $attachedIdList, int $result) use (\October\Rain\Database\Model $model) { + * traceLog("{$result} entries were detached for Relation {$relationName}"); * }); * */ - $this->parent->fireEvent('model.relation.afterDetach', [$this->relationName, $attachedIdList]); + $this->parent->fireEvent('model.relation.afterDetach', [$this->relationName, $attachedIdList, $result]); return $result; } From 4b015d4162be839cd02a2d3780b6c1db0f7d52db Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 3 Nov 2020 09:27:37 -0500 Subject: [PATCH 13/27] add {before,after}Add events to AttachOneOrMany --- src/Database/Relations/AttachOneOrMany.php | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Database/Relations/AttachOneOrMany.php b/src/Database/Relations/AttachOneOrMany.php index 7ab21a28b..cc3338b89 100644 --- a/src/Database/Relations/AttachOneOrMany.php +++ b/src/Database/Relations/AttachOneOrMany.php @@ -166,6 +166,21 @@ public function add(Model $model, $sessionKey = null) } if ($sessionKey === null) { + /** + * @event model.relation.beforeAdd + * Called before adding a relation to the model (only for AttachOne/AttachMany 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, $this->related]); + // Delete siblings for single attachments if ($this instanceof AttachOne) { $this->delete(); @@ -185,6 +200,20 @@ 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 (only for AttachOne/AttachMany 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); + * traceLog("Relation {$relationName} was added to model {$relatedClass}."); + * }); + * + */ + $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $this->related]); } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); From 4538d8078e4bce3188c15e64ea587db17fe7c55d Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 3 Nov 2020 13:55:02 -0500 Subject: [PATCH 14/27] fix examples --- src/Database/Relations/AttachOneOrMany.php | 3 ++- src/Database/Relations/BelongsOrMorphTo.php | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Database/Relations/AttachOneOrMany.php b/src/Database/Relations/AttachOneOrMany.php index cc3338b89..f26a67391 100644 --- a/src/Database/Relations/AttachOneOrMany.php +++ b/src/Database/Relations/AttachOneOrMany.php @@ -209,7 +209,8 @@ public function add(Model $model, $sessionKey = null) * * $model->bindEvent('model.relation.afterAdd', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { * $relatedClass = get_class($relatedModel); - * traceLog("Relation {$relationName} was added to model {$relatedClass}."); + * $modelClass = get_class($relatedModel); + * traceLog("{$relatedClass} was added as {$relationName} to {$modelClass}."); * }); * */ diff --git a/src/Database/Relations/BelongsOrMorphTo.php b/src/Database/Relations/BelongsOrMorphTo.php index d5ddde8c5..27c07c256 100644 --- a/src/Database/Relations/BelongsOrMorphTo.php +++ b/src/Database/Relations/BelongsOrMorphTo.php @@ -35,7 +35,8 @@ public function associate($model) * * $model->bindEvent('model.relation.afterAssociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { * $relatedClass = get_class($relatedModel); - * traceLog("Relation {$relationName} was associated to model {$relatedClass}."); + * $modelClass = get_class($model); + * traceLog("{$relatedClass} was associated as {$relationName} to {$modelClass}."); * }); * */ @@ -76,7 +77,8 @@ public function dissociate() * * $model->bindEvent('model.relation.afterDissociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { * $relatedClass = get_class($relatedModel); - * traceLog("Relation {$relationName} was dissociated from model {$relatedClass}."); + * $modelClass = get_class($model); + * traceLog("{$relatedClass} was dissociated from {$modelClass}."); * }); * */ From 40215d0368aec32815c8300bc80e91250c46333c Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Tue, 3 Nov 2020 14:00:27 -0500 Subject: [PATCH 15/27] fix typo --- src/Database/Relations/AttachOneOrMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Relations/AttachOneOrMany.php b/src/Database/Relations/AttachOneOrMany.php index f26a67391..69849b1cc 100644 --- a/src/Database/Relations/AttachOneOrMany.php +++ b/src/Database/Relations/AttachOneOrMany.php @@ -209,7 +209,7 @@ public function add(Model $model, $sessionKey = null) * * $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($relatedModel); + * $modelClass = get_class($model); * traceLog("{$relatedClass} was added as {$relationName} to {$modelClass}."); * }); * From 24870fc8c51064beea4b0ce3be8353222c0b311d Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 6 Nov 2020 13:33:38 -0500 Subject: [PATCH 16/27] add remainging events --- src/Database/Relations/AttachOneOrMany.php | 34 +++++++++++- src/Database/Relations/HasOneOrMany.php | 59 +++++++++++++++++++++ src/Database/Relations/MorphOneOrMany.php | 60 ++++++++++++++++++++++ 3 files changed, 151 insertions(+), 2 deletions(-) diff --git a/src/Database/Relations/AttachOneOrMany.php b/src/Database/Relations/AttachOneOrMany.php index 69849b1cc..92e251ac8 100644 --- a/src/Database/Relations/AttachOneOrMany.php +++ b/src/Database/Relations/AttachOneOrMany.php @@ -168,7 +168,7 @@ public function add(Model $model, $sessionKey = null) if ($sessionKey === null) { /** * @event model.relation.beforeAdd - * Called before adding a relation to the model (only for AttachOne/AttachMany relations) + * Called before adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations) * * Example usage: * @@ -203,7 +203,7 @@ public function add(Model $model, $sessionKey = null) /** * @event model.relation.afterAdd - * Called after adding a relation to the model (only for AttachOne/AttachMany relations) + * Called after adding a relation to the model (for AttachOneOrMany, HasOneOrMany & MorphOneOrMany relations) * * Example usage: * @@ -239,6 +239,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, $this->related]); + $options = $this->parent->getRelationDefinition($this->relationName); if (array_get($options, 'delete', false)) { @@ -263,6 +278,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, $this->related]); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); diff --git a/src/Database/Relations/HasOneOrMany.php b/src/Database/Relations/HasOneOrMany.php index 7d2325257..f8c541ad5 100644 --- a/src/Database/Relations/HasOneOrMany.php +++ b/src/Database/Relations/HasOneOrMany.php @@ -56,6 +56,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, $this->related]); + $model->setAttribute($this->getForeignKeyName(), $this->getParentKey()); if (!$model->exists || $model->isDirty()) { @@ -71,6 +86,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, $this->related]); } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); @@ -95,6 +125,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, $this->related]); + $model->setAttribute($this->getForeignKeyName(), null); $model->save(); @@ -107,6 +152,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, $this->related]); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); diff --git a/src/Database/Relations/MorphOneOrMany.php b/src/Database/Relations/MorphOneOrMany.php index 552b1a1d9..a56849ddf 100644 --- a/src/Database/Relations/MorphOneOrMany.php +++ b/src/Database/Relations/MorphOneOrMany.php @@ -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, $this->related]); + $model->setAttribute($this->getForeignKeyName(), $this->getParentKey()); $model->setAttribute($this->getMorphType(), $this->morphClass); $model->save(); @@ -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, $this->related]); } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); @@ -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, $this->related]); + $options = $this->parent->getRelationDefinition($this->relationName); if (array_get($options, 'delete', false)) { @@ -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, $this->related]); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); From 5c73f26ff4362a03cd24e0512afd31cdfe162b78 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 8 Nov 2020 11:16:02 -0500 Subject: [PATCH 17/27] call parent::attach() instead of duplicating code here --- src/Database/Relations/BelongsToMany.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index d76cd9281..59bfccad3 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -129,14 +129,10 @@ public function attach($id, array $attributes = [], $touch = true) */ $this->parent->fireEvent('model.relation.beforeAttach', [$this->relationName, $attachedIdList, $insertData]); - // Here we will insert the attachment records into the pivot table. Once we have - // inserted the records, we will touch the relationships if necessary and the - // function will return. We can parse the IDs before inserting the records. - $this->newPivotStatement()->insert($insertData); - - if ($touch) { - $this->touchIfTouching(); - } + /* + * See Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable + */ + $result = parent::attach($ids, $attributes, $touch); /** * @event model.relation.afterAttach From 36a6344173db936cb53073e8bf1da3af9e0982b1 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 8 Nov 2020 11:42:09 -0500 Subject: [PATCH 18/27] belongsto and morphto associate() methods have potentially different arguments signature --- src/Database/Relations/BelongsTo.php | 86 +++++++++++++++++++++++++++- src/Database/Relations/MorphTo.php | 86 +++++++++++++++++++++++++++- 2 files changed, 170 insertions(+), 2 deletions(-) diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index da5373b38..4a33171f4 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -8,7 +8,6 @@ class BelongsTo extends BelongsToBase { use DeferOneOrMany; use DefinedConstraints; - use BelongsOrMorphTo; /** * @var string The "name" of the relationship. @@ -50,6 +49,91 @@ 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, $this->related]); + + $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, $this->related]); + + 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, \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.beforeDissociate', [$this->relationName, $this->related]); + + $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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * $relatedClass = get_class($relatedModel); + * $modelClass = get_class($model); + * traceLog("{$relatedClass} was dissociated from {$modelClass}."); + * }); + * + */ + $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); + + return $result; + } + /** * Helper for setting this relationship using various expected * values. For example, $model->relation = $value; diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index dd7ec8ec0..a06dd3316 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -7,7 +7,6 @@ class MorphTo extends MorphToBase { use DefinedConstraints; - use BelongsOrMorphTo; /** * @var string The "name" of the relationship. @@ -23,6 +22,91 @@ public function __construct(Builder $query, Model $parent, $foreignKey, $otherKe $this->addDefinedConstraints(); } + /** + * Associate the model instance to the given parent. + * + * @param \Illuminate\Database\Eloquent\Model $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, $this->related]); + + $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, $this->related]); + + 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, \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.beforeDissociate', [$this->relationName, $this->related]); + + $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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * $relatedClass = get_class($relatedModel); + * $modelClass = get_class($model); + * traceLog("{$relatedClass} was dissociated from {$modelClass}."); + * }); + * + */ + $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); + + return $result; + } + /** * Helper for setting this relationship using various expected * values. For example, $model->relation = $value; From 6d333b9c6c94fa772a310200140f4dcdc465d96e Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 8 Nov 2020 11:45:30 -0500 Subject: [PATCH 19/27] not needed, code moved to each relations --- src/Database/Relations/BelongsOrMorphTo.php | 89 --------------------- 1 file changed, 89 deletions(-) delete mode 100644 src/Database/Relations/BelongsOrMorphTo.php diff --git a/src/Database/Relations/BelongsOrMorphTo.php b/src/Database/Relations/BelongsOrMorphTo.php deleted file mode 100644 index 27c07c256..000000000 --- a/src/Database/Relations/BelongsOrMorphTo.php +++ /dev/null @@ -1,89 +0,0 @@ -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, $this->related]); - - $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, $this->related]); - - 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, \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.beforeDissociate', [$this->relationName, $this->related]); - - $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, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * $relatedClass = get_class($relatedModel); - * $modelClass = get_class($model); - * traceLog("{$relatedClass} was dissociated from {$modelClass}."); - * }); - * - */ - $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); - - return $result; - } -} From 69019c01da0067e7f6c8e5e7f8a640c33e2d8e76 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 8 Nov 2020 12:13:08 -0500 Subject: [PATCH 20/27] properly pass the related model when firing the event --- src/Database/Relations/BelongsTo.php | 4 ++-- src/Database/Relations/HasOneOrMany.php | 8 ++++---- src/Database/Relations/MorphOneOrMany.php | 8 ++++---- src/Database/Relations/MorphTo.php | 2 +- 4 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index 4a33171f4..cb078def9 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -70,7 +70,7 @@ public function associate($model) * }); * */ - $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $model]); $result = parent::associate($model); @@ -87,7 +87,7 @@ public function associate($model) * }); * */ - $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $model]); return $result; } diff --git a/src/Database/Relations/HasOneOrMany.php b/src/Database/Relations/HasOneOrMany.php index f8c541ad5..fd3283168 100644 --- a/src/Database/Relations/HasOneOrMany.php +++ b/src/Database/Relations/HasOneOrMany.php @@ -69,7 +69,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $model]); $model->setAttribute($this->getForeignKeyName(), $this->getParentKey()); @@ -100,7 +100,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]); } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); @@ -138,7 +138,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $model]); $model->setAttribute($this->getForeignKeyName(), null); $model->save(); @@ -165,7 +165,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $model]); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); diff --git a/src/Database/Relations/MorphOneOrMany.php b/src/Database/Relations/MorphOneOrMany.php index a56849ddf..26f763581 100644 --- a/src/Database/Relations/MorphOneOrMany.php +++ b/src/Database/Relations/MorphOneOrMany.php @@ -58,7 +58,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $model]); $model->setAttribute($this->getForeignKeyName(), $this->getParentKey()); $model->setAttribute($this->getMorphType(), $this->morphClass); @@ -87,7 +87,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]); } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); @@ -113,7 +113,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $model]); $options = $this->parent->getRelationDefinition($this->relationName); @@ -152,7 +152,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $model]); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index a06dd3316..f362e453b 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -43,7 +43,7 @@ public function associate($model) * }); * */ - $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeAssociate', [$this->relationName, $model]); $result = parent::associate($model); From 70bff296ac5ee3b07da67e70dc0620babb95c6b9 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sun, 8 Nov 2020 17:26:44 -0500 Subject: [PATCH 21/27] remove relatedModel event argument for dissociate() --- src/Database/Relations/BelongsTo.php | 11 +++++------ src/Database/Relations/MorphTo.php | 11 +++++------ 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/Database/Relations/BelongsTo.php b/src/Database/Relations/BelongsTo.php index cb078def9..4983931cb 100644 --- a/src/Database/Relations/BelongsTo.php +++ b/src/Database/Relations/BelongsTo.php @@ -105,14 +105,14 @@ public function dissociate() * * Example usage: * - * $model->bindEvent('model.relation.beforeDissociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * $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, $this->related]); + $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName]); $result = parent::dissociate(); @@ -122,14 +122,13 @@ public function dissociate() * * Example usage: * - * $model->bindEvent('model.relation.afterDissociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * $relatedClass = get_class($relatedModel); + * $model->bindEvent('model.relation.afterDissociate', function (string $relationName) use (\October\Rain\Database\Model $model) { * $modelClass = get_class($model); - * traceLog("{$relatedClass} was dissociated from {$modelClass}."); + * traceLog("{$relationName} was dissociated from {$modelClass}."); * }); * */ - $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName]); return $result; } diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index f362e453b..89d3b6de9 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -78,14 +78,14 @@ public function dissociate() * * Example usage: * - * $model->bindEvent('model.relation.beforeDissociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { + * $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, $this->related]); + $this->parent->fireEvent('model.relation.beforeDissociate', [$this->relationName]); $result = parent::dissociate(); @@ -95,14 +95,13 @@ public function dissociate() * * Example usage: * - * $model->bindEvent('model.relation.afterDissociate', function (string $relationName, \October\Rain\Database\Model $relatedModel) use (\October\Rain\Database\Model $model) { - * $relatedClass = get_class($relatedModel); + * $model->bindEvent('model.relation.afterDissociate', function (string $relationName) use (\October\Rain\Database\Model $model) { * $modelClass = get_class($model); - * traceLog("{$relatedClass} was dissociated from {$modelClass}."); + * traceLog("{$relationName} was dissociated from {$modelClass}."); * }); * */ - $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterDissociate', [$this->relationName]); return $result; } From 5142160bb919ab58a3dd650df98263d217691d7f Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 9 Nov 2020 06:36:13 -0500 Subject: [PATCH 22/27] properly pass the related model when firing the event --- src/Database/Relations/AttachOneOrMany.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/Relations/AttachOneOrMany.php b/src/Database/Relations/AttachOneOrMany.php index 92e251ac8..d7dd1fd9b 100644 --- a/src/Database/Relations/AttachOneOrMany.php +++ b/src/Database/Relations/AttachOneOrMany.php @@ -179,7 +179,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $this->model]); // Delete siblings for single attachments if ($this instanceof AttachOne) { @@ -214,7 +214,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $this->model]); } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); @@ -252,7 +252,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $this->model]); $options = $this->parent->getRelationDefinition($this->relationName); @@ -292,7 +292,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $this->model]); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); From 4601ae3900276455e26434d002e6dfeb218821b8 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 9 Nov 2020 09:29:57 -0500 Subject: [PATCH 23/27] use $model method argument, not $this->model --- src/Database/Relations/AttachOneOrMany.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/Relations/AttachOneOrMany.php b/src/Database/Relations/AttachOneOrMany.php index d7dd1fd9b..7bb8234d1 100644 --- a/src/Database/Relations/AttachOneOrMany.php +++ b/src/Database/Relations/AttachOneOrMany.php @@ -179,7 +179,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $this->model]); + $this->parent->fireEvent('model.relation.beforeAdd', [$this->relationName, $model]); // Delete siblings for single attachments if ($this instanceof AttachOne) { @@ -214,7 +214,7 @@ public function add(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $this->model]); + $this->parent->fireEvent('model.relation.afterAdd', [$this->relationName, $model]); } else { $this->parent->bindDeferred($this->relationName, $model, $sessionKey); @@ -252,7 +252,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $this->model]); + $this->parent->fireEvent('model.relation.beforeRemove', [$this->relationName, $model]); $options = $this->parent->getRelationDefinition($this->relationName); @@ -292,7 +292,7 @@ public function remove(Model $model, $sessionKey = null) * }); * */ - $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $this->model]); + $this->parent->fireEvent('model.relation.afterRemove', [$this->relationName, $model]); } else { $this->parent->unbindDeferred($this->relationName, $model, $sessionKey); From 1812390a7fe28d88cb76e18b6023b3ae4221def6 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 9 Nov 2020 11:06:11 -0500 Subject: [PATCH 24/27] fix related model --- src/Database/Relations/MorphTo.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Relations/MorphTo.php b/src/Database/Relations/MorphTo.php index 89d3b6de9..79d41b4c2 100644 --- a/src/Database/Relations/MorphTo.php +++ b/src/Database/Relations/MorphTo.php @@ -60,7 +60,7 @@ public function associate($model) * }); * */ - $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $this->related]); + $this->parent->fireEvent('model.relation.afterAssociate', [$this->relationName, $model]); return $result; } From 1c62d591b63c1b866e9f701eb5f23ff8f96845ff Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Nov 2020 10:06:55 -0500 Subject: [PATCH 25/27] no result returned from parent --- src/Database/Relations/BelongsToMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index a8c741519..05d5e6c9a 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -132,7 +132,7 @@ public function attach($id, array $attributes = [], $touch = true) /* * See Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable */ - $result = parent::attach($ids, $attributes, $touch); + parent::attach($ids, $attributes, $touch); /** * @event model.relation.afterAttach From 3d090077b9042adba6bb15b28523c0e5079748e3 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Sat, 21 Nov 2020 10:11:24 -0500 Subject: [PATCH 26/27] fix typo --- src/Database/Relations/BelongsToMany.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index 05d5e6c9a..879f94d4e 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -132,7 +132,7 @@ public function attach($id, array $attributes = [], $touch = true) /* * See Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable */ - parent::attach($ids, $attributes, $touch); + parent::attach($id, $attributes, $touch); /** * @event model.relation.afterAttach From 51698b6f7c2fcac2b5413f04540dfa2665cc7c50 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 8 Feb 2021 00:21:24 -0500 Subject: [PATCH 27/27] Update src/Database/Relations/BelongsToMany.php Co-authored-by: Ben Thomson --- src/Database/Relations/BelongsToMany.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Relations/BelongsToMany.php b/src/Database/Relations/BelongsToMany.php index 429c906e0..475e827cc 100644 --- a/src/Database/Relations/BelongsToMany.php +++ b/src/Database/Relations/BelongsToMany.php @@ -141,8 +141,8 @@ public function attach($id, array $attributes = [], $touch = true) */ $this->parent->fireEvent('model.relation.beforeAttach', [$this->relationName, $attachedIdList, $insertData]); - /* - * @See Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable + /** + * @see \Illuminate\Database\Eloquent\Relations\Concerns\InteractsWithPivotTable */ parent::attach($id, $attributes, $touch);