-
Notifications
You must be signed in to change notification settings - Fork 274
Commit
Add documentation for the new DAL field type EnumField.
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
--- | ||
nav: | ||
title: EnumField usage | ||
position: 100 | ||
|
||
--- | ||
|
||
# EnumField usage | ||
|
||
## Usage | ||
|
||
The `EnumField` can be used to restrict `string` or `int` values to a fixed set. | ||
|
||
Define a `\BackedEnum` class, use them in an Entity and restrict the values in your RDBMS. | ||
|
||
<Tabs> | ||
<Tab title="BackedEnums"> | ||
|
||
```php | ||
Check warning on line 19 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
<?php | ||
|
||
enum PaymentMethod : string { | ||
case PAYPAL = 'paypal'; | ||
Check warning on line 23 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
case CREDIT_CARD = 'credit_card'; | ||
case INVOICE = 'invoice'; | ||
} | ||
``` | ||
|
||
```php | ||
Check warning on line 29 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
<?php | ||
|
||
enum BatchOrderSize: int { | ||
case DOZEN = 12; | ||
Check warning on line 33 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
case SCORE = 20; | ||
case SMALL_GROSS = 120; | ||
case GROSS = 144; | ||
case GRAND = 1000; | ||
} | ||
``` | ||
|
||
</Tab> | ||
<Tab title="Entity usage"> | ||
|
||
```php | ||
Check warning on line 44 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
<?php | ||
|
||
class BatchOrderEntity extends Entity { | ||
|
||
#[Field(type: FieldType::ENUM, column: 'payment_method')] | ||
protected PaymentMethod $paymentMethod; | ||
|
||
#[Field(type: FieldType::ENUM, column: 'amount')] | ||
protected BatchOrderSize $amount; | ||
|
||
public function getPaymentMethod(): PaymentMethod | ||
{ | ||
return $this->paymentMethod; | ||
} | ||
|
||
public function setPaymentMethod(PaymentMethod $paymentMethod): void | ||
{ | ||
$this->paymentMethod = $paymentMethod; | ||
} | ||
|
||
public function getAmount(): BatchOrderSize | ||
{ | ||
return $this->amount; | ||
} | ||
|
||
public function setAmount(BatchOrderSize $amount): void | ||
{ | ||
$this->amount = $amount; | ||
} | ||
``` | ||
|
||
</Tab> | ||
<Tab title="RDBMS definition"> | ||
|
||
```sql | ||
CREATE TABLE `batch_order` ( | ||
`id` BINARY(16) NOT NULL, | ||
`payment_method` ENUM('paypal', 'credit_card', 'invoice') NOT NULL, | ||
Check warning on line 82 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
`amount` INT NOT NULL, | ||
PRIMARY KEY (`id`) | ||
); | ||
``` | ||
|
||
</Tab> | ||
</Tabs> | ||
|
||
It's not advisable to use `ENUM` types for integer values, as most RDBMS only support string values and use integers | ||
internally. Using a regular `INT` column is recommended in this case. The `BackedEnum` will restrict the possible | ||
values, unless the database is modified manually. | ||
|
||
## Examples | ||
|
||
### Example 1: Creating an input field from an enum | ||
|
||
```twig | ||
<select name="payment_method"> | ||
{% for method in PaymentMethod::cases() %} | ||
<option value="{{ method.value }}">{{ method.name }}</option> | ||
Check warning on line 102 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
{% endfor %} | ||
</select> | ||
``` | ||
|
||
### Example 2: Setting an Entity value | ||
|
||
```php | ||
Check warning on line 109 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
<?php | ||
|
||
$batchOrder = new BatchOrderEntity(); | ||
$batchOrder->setPaymentMethod(PaymentMethod::PAYPAL); | ||
``` | ||
|
||
### Example 3: Check if a value is valid | ||
|
||
```php | ||
Check warning on line 118 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md
|
||
<?php | ||
|
||
$validPaymentMethod = PaymentMethod::tryFrom($userProvidedInput); | ||
|
||
// Either check for null | ||
if (is_null($validPaymentMethod)) { | ||
// The input was not a valid payment method | ||
} | ||
|
||
// Or check for the class | ||
if($validPaymentMethod instanceof PaymentMethod) { | ||
// The input was a valid payment method | ||
} | ||
|
||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
--- | ||
nav: | ||
title: Fields Reference | ||
position: 10 | ||
|
||
--- | ||
|
||
# Fields Reference | ||
|
||
| Name | Description | | ||
|:-----------------------------|:-----------------------------------------| | ||
| BlacklistRuleField | Stores a "BlacklistRule" value | | ||
| BlobField | Stores a "Blob" value | | ||
| BoolField | Stores a "Bool" value | | ||
| CalculatedPriceField | Stores a "CalculatedPrice" value | | ||
| CartPriceField | Stores a "CartPrice" value | | ||
| ChildCountField | Stores a "ChildCount" value | | ||
| ChildrenAssociationField | Stores a "ChildrenAssociation" value | | ||
| ConfigJsonField | Stores a "ConfigJson" value | | ||
| CreatedAtField | Stores a "CreatedAt" value | | ||
| CreatedByField | Stores a "CreatedBy" value | | ||
| CustomField | Stores a "Custom" value | | ||
| DateField | Stores a "Date" value | | ||
| DateTimeField | Stores a "DateTime" value | | ||
| EmailField | Stores a "Email" value | | ||
Check warning on line 25 in resources/references/core-reference/dal-reference/fields-reference/index.md
|
||
| [EnumField](enum-field) | Stores a "BackedEnum" value | | ||
| FkField | Stores a "Fk" value | | ||
| FloatField | Stores a "Float" value | | ||
| IdField | Stores a "Id" value | | ||
Check warning on line 29 in resources/references/core-reference/dal-reference/fields-reference/index.md
|
||
| IntField | Stores a "Int" value | | ||
Check warning on line 30 in resources/references/core-reference/dal-reference/fields-reference/index.md
|
||
| JsonField | Stores a "Json" value | | ||
| ListField | Stores a "List" value | | ||
| ListingPriceField | Stores a "ListingPrice" value | | ||
| LockedField | Stores a "Locked" value | | ||
| LongTextField | Stores a "LongText" value | | ||
| ManyToManyAssociationField | Stores a "ManyToManyAssociation" value | | ||
| ManyToManyIdField | Stores a "ManyToManyId" value | | ||
| ManyToOneAssociationField | Stores a "ManyToOneAssociation" value | | ||
| ObjectField | Stores a "Object" value | | ||
Check warning on line 39 in resources/references/core-reference/dal-reference/fields-reference/index.md
|
||
| OneToManyAssociationField | Stores a "OneToManyAssociation" value | | ||
| OneToOneAssociationField | Stores a "OneToOneAssociation" value | | ||
| ParentAssociationField | Stores a "ParentAssociation" value | | ||
| ParentFkField | Stores a "ParentFk" value | | ||
| PasswordField | Stores a "Password" value | | ||
| PriceDefinitionField | Stores a "PriceDefinition" value | | ||
| PriceField | Stores a "Price" value | | ||
| ReferenceVersionField | Stores a "ReferenceVersion" value | | ||
| RemoteAddressField | Stores a "RemoteAddress" value | | ||
| StateMachineStateField | Stores a "StateMachineState" value | | ||
| StringField | Stores a "String" value | | ||
| TranslatedField | Stores a "Translated" value | | ||
| TranslationsAssociationField | Stores a "TranslationsAssociation" value | | ||
| TreeBreadcrumbField | Stores a "TreeBreadcrumb" value | | ||
| TreeLevelField | Stores a "TreeLevel" value | | ||
| TreePathField | Stores a "TreePath" value | | ||
| UpdatedAtField | Stores a "UpdatedAt" value | | ||
| UpdatedByField | Stores a "UpdatedBy" value | | ||
| VersionDataPayloadField | Stores a "VersionDataPayload" value | | ||
| VersionField | Stores a "Version" value | | ||
| WhitelistRuleField | Stores a "WhitelistRule" value | |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. | ||
# yarn lockfile v1 | ||
|
||
|