Skip to content
Merged
Changes from all commits
Commits
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
36 changes: 36 additions & 0 deletions database/model.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -710,3 +712,37 @@ Additionally, a few methods exist to extend protected model properties.
$model->addJsonable('some_data');
});
```

### Relationships methods

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(string $name, array $config)
public function addHasManyRelation(string $name, array $config)

public function addBelongsToRelation(string $name, array $config)
public function addBelongsToManyRelation(string $name, array $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(string $name, array $config)
public function addAttachManyRelation(string $name, array $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.

Example usage:

```php
\Backend\Models\User::extend(function($model) {
$model->addHasOne('profile', ['Acme\Demo\Models\Profile', 'key' => 'user_id']);
});
```