-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2aad221
commit b8fb109
Showing
8 changed files
with
1,052 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
name: control-api | ||
title: Control (API) | ||
version: develop | ||
start_page: developers:index.adoc | ||
nav: | ||
- modules/developers/nav.adoc |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
* xref:index.adoc[Home] | ||
* xref:integrations.adoc[Integrations] | ||
* xref:api.adoc[API] | ||
* xref:additional-attributes.adoc[Additional Attributes] |
36 changes: 36 additions & 0 deletions
36
antora-docs/modules/developers/pages/additional-attributes.adoc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
= Additional Attributes | ||
|
||
To increase the flexibility of the Data Provider, we'd like to add | ||
arbitrary properties. These can't be defined as their own column in the | ||
database, because at some point they will be settable through the UI. | ||
Therefore, we use the 'HasAdditionalProperties' trait. | ||
|
||
This trait relies on storing the additional properties in a single | ||
column, a JSON column called 'additional_attributes'. | ||
|
||
.... | ||
$table->json('additional_attributes')->nullable()->default('[]'); | ||
.... | ||
|
||
In the service provider, we define the additional properties the model | ||
can handle. This is done through the `+Model::addProperty($key)+` method | ||
call. Two methods are then defined, `+getAdditionalAttribute($key)+` and | ||
`+setAdditionalAttribute($key, $value)+`. These retrieve the | ||
additional_attributes, using the defined mutator to ensure it is an | ||
array, then return or set the correct values. | ||
|
||
To let us keep doing things the 'Laravel' way, we also override the get | ||
and setAttribute functions. Before the default implementations of these | ||
methods are called, we check to see if the user is getting or setting an | ||
'additional property'. If they are, we pass the method call onto the | ||
corresponding set or get additionalAttribute function, otherwise we | ||
continue with the default method call. | ||
|
||
Finally, we want to automatically enter these parameters into the model | ||
when cast to an array. To do that, the trait automatically adds | ||
additional properties to the appends array. We then define a __call | ||
method, which handles the method call if it's an accessor or mutator | ||
attempt for an additional property. | ||
Other models must define the get and setAdditonalAttribte methods, and | ||
return the additional attributes when casted to an array or a string. |
Oops, something went wrong.