From 5bd1d7f2c4da14d3f1ef51d30d55c6747c5a1f75 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 7 Oct 2022 09:00:53 -0400 Subject: [PATCH 1/3] Document HasRelationships trait methods --- database/model.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/database/model.md b/database/model.md index 977d3912..f6526252 100644 --- a/database/model.md +++ b/database/model.md @@ -710,3 +710,35 @@ Additionally, a few methods exist to extend protected model properties. $model->addJsonable('some_data'); }); ``` + +The following methods are provided by the [HasRelationship](https://github.com/wintercms/storm/blob/develop/src/Database/Concerns/HasRelationships.php) trait: + +```php +public function addHasOneRelation($name, $config) +public function addHasManyRelation($name, $config) + +public function addBelongsToRelation($name, $config) +public function addBelongsToManyRelation($name, $config) + +public function addMorphToRelation($name, $config) +public function addMorphOneRelation($name, $config) +public function addMorphManyRelation($name, $config) +public function addMorphToManyRelation($name, $config) +public function addMorphedByManyRelation($name, $config) + +public function addAttachOneRelation($name, $config) +public function addAttachManyRelation($name, $config) + +public function addHasOneThroughRelation($name, $config) +public function addHasManyThroughRelation($name, $config) +``` + +It is strongly suggested to use the above methods to add relations when extending a model since they will merge the existing relations and make sure the relation is valid and does not already exit. + +Example usage: + +```php +\Backend\Models\User::extend(function($model) { + $model->addHasOne('profile', ['Acme\Demo\Models\Profile', 'key' => 'user_id']); +}); +``` From fb5e0bee3e5eedfb3e47c4a4427499272c06917d Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Fri, 7 Oct 2022 23:33:08 -0400 Subject: [PATCH 2/3] add ToC links --- database/model.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/database/model.md b/database/model.md index f6526252..ad59fa53 100644 --- a/database/model.md +++ b/database/model.md @@ -692,6 +692,8 @@ This approach can also be used to bind to [local events](#events), the following > **NOTE:** Typically the best place to place code is within your plugin registration class `boot` method as this will be run on every request ensuring that the extensions you make to the model are available everywhere. +### Property methods + Additionally, a few methods exist to extend protected model properties. ```php @@ -711,6 +713,8 @@ Additionally, a few methods exist to extend protected model properties. }); ``` +### Relationships methods + The following methods are provided by the [HasRelationship](https://github.com/wintercms/storm/blob/develop/src/Database/Concerns/HasRelationships.php) trait: ```php From 851bc25823e98dfeeb8c8d2a95601aa9cce464d5 Mon Sep 17 00:00:00 2001 From: Marc Jauvin Date: Mon, 20 Feb 2023 07:56:17 -0500 Subject: [PATCH 3/3] add type hints --- database/model.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/database/model.md b/database/model.md index ad59fa53..fe15855d 100644 --- a/database/model.md +++ b/database/model.md @@ -718,23 +718,23 @@ Additionally, a few methods exist to extend protected model properties. The following methods are provided by the [HasRelationship](https://github.com/wintercms/storm/blob/develop/src/Database/Concerns/HasRelationships.php) trait: ```php -public function addHasOneRelation($name, $config) -public function addHasManyRelation($name, $config) +public function addHasOneRelation(string $name, array $config) +public function addHasManyRelation(string $name, array $config) -public function addBelongsToRelation($name, $config) -public function addBelongsToManyRelation($name, $config) +public function addBelongsToRelation(string $name, array $config) +public function addBelongsToManyRelation(string $name, array $config) -public function addMorphToRelation($name, $config) -public function addMorphOneRelation($name, $config) -public function addMorphManyRelation($name, $config) -public function addMorphToManyRelation($name, $config) -public function addMorphedByManyRelation($name, $config) +public function addMorphToRelation(string $name, array $config) +public function addMorphOneRelation(string $name, array $config) +public function addMorphManyRelation(string $name, array $config) +public function addMorphToManyRelation(string $name, array $config) +public function addMorphedByManyRelation(string $name, array $config) -public function addAttachOneRelation($name, $config) -public function addAttachManyRelation($name, $config) +public function addAttachOneRelation(string $name, array $config) +public function addAttachManyRelation(string $name, array $config) -public function addHasOneThroughRelation($name, $config) -public function addHasManyThroughRelation($name, $config) +public function addHasOneThroughRelation(string $name, array $config) +public function addHasManyThroughRelation(string $name, array $config) ``` It is strongly suggested to use the above methods to add relations when extending a model since they will merge the existing relations and make sure the relation is valid and does not already exit.