Skip to content

Commit

Permalink
feat: Initial docs for DAL EnumField
Browse files Browse the repository at this point in the history
Add documentation for the new DAL field type EnumField.
  • Loading branch information
dgrothaus-sw committed Jan 17, 2025
1 parent 616f626 commit 744d71b
Showing 9 changed files with 208 additions and 59 deletions.
Binary file added .devenv/nix-eval-cache.db
Binary file not shown.
Binary file added .devenv/nix-eval-cache.db-shm
Binary file not shown.
Binary file added .devenv/nix-eval-cache.db-wal
Binary file not shown.
1 change: 1 addition & 0 deletions .wordlist.txt
Original file line number Diff line number Diff line change
@@ -305,6 +305,7 @@ EntitySearchResult
EntityWrittenContainerEvent
EntityWrittenEvent
Enum
EnumField
Enums
EqualsAny
ErrorsFacade
10 changes: 10 additions & 0 deletions node_modules/.yarn-integrity

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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L19

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:19:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<?php

enum PaymentMethod : string {
case PAYPAL = 'paypal';

Check warning on line 23 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L23

The official name of this payment provider is spelled with two capital “P”. (PAYPAL[1]) Suggestions: `PayPal` URL: https://en.wikipedia.org/wiki/PayPal Rule: https://community.languagetool.org/rule/show/PAYPAL?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:23:19: The official name of this payment provider is spelled with two capital “P”. (PAYPAL[1])
 Suggestions: `PayPal`
 URL: https://en.wikipedia.org/wiki/PayPal 
 Rule: https://community.languagetool.org/rule/show/PAYPAL?lang=en-US&subId=1
 Category: CASING
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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L29

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:29:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<?php

enum BatchOrderSize: int {
case DOZEN = 12;

Check warning on line 33 in resources/references/core-reference/dal-reference/fields-reference/enum-field.md

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L33

Use “a DOZEN”, or use a number before ‘DOZEN’. (NODT_DOZEN[1]) Suggestions: `A DOZEN` Rule: https://community.languagetool.org/rule/show/NODT_DOZEN?lang=en-US&subId=1 Category: GRAMMAR
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:33:9: Use “a DOZEN”, or use a number before ‘DOZEN’. (NODT_DOZEN[1])
 Suggestions: `A DOZEN`
 Rule: https://community.languagetool.org/rule/show/NODT_DOZEN?lang=en-US&subId=1
 Category: GRAMMAR
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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L44

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:44:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<?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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L82

The official name of this payment provider is spelled with two capital “P”. (PAYPAL[1]) Suggestions: `PayPal` URL: https://en.wikipedia.org/wiki/PayPal Rule: https://community.languagetool.org/rule/show/PAYPAL?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:82:27: The official name of this payment provider is spelled with two capital “P”. (PAYPAL[1])
 Suggestions: `PayPal`
 URL: https://en.wikipedia.org/wiki/PayPal 
 Rule: https://community.languagetool.org/rule/show/PAYPAL?lang=en-US&subId=1
 Category: CASING
`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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L102

If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1]) Suggestions: ` Value`, ` value` Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:102:33: If a new sentence starts here, add a space and start with an uppercase letter. (LC_AFTER_PERIOD[1])
 Suggestions: ` Value`, ` value`
 Rule: https://community.languagetool.org/rule/show/LC_AFTER_PERIOD?lang=en-US&subId=1
 Category: CASING
{% 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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L109

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:109:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<?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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/enum-field.md#L118

File types are normally capitalized. (FILE_EXTENSIONS_CASE[1]) Suggestions: `PHP` URL: https://languagetool.org/insights/post/spelling-capital-letters/ Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1 Category: CASING
Raw output
resources/references/core-reference/dal-reference/fields-reference/enum-field.md:118:3: File types are normally capitalized. (FILE_EXTENSIONS_CASE[1])
 Suggestions: `PHP`
 URL: https://languagetool.org/insights/post/spelling-capital-letters/ 
 Rule: https://community.languagetool.org/rule/show/FILE_EXTENSIONS_CASE?lang=en-US&subId=1
 Category: CASING
<?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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/index.md#L25

Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN) Suggestions: `an` URL: https://languagetool.org/insights/post/indefinite-articles/ Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US Category: MISC
Raw output
resources/references/core-reference/dal-reference/fields-reference/index.md:25:40: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN)
 Suggestions: `an`
 URL: https://languagetool.org/insights/post/indefinite-articles/ 
 Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US
 Category: MISC
| [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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/index.md#L29

Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN) Suggestions: `an` URL: https://languagetool.org/insights/post/indefinite-articles/ Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US Category: MISC
Raw output
resources/references/core-reference/dal-reference/fields-reference/index.md:29:40: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN)
 Suggestions: `an`
 URL: https://languagetool.org/insights/post/indefinite-articles/ 
 Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US
 Category: MISC
| IntField | Stores a "Int" value |

Check warning on line 30 in resources/references/core-reference/dal-reference/fields-reference/index.md

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/index.md#L30

Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN) Suggestions: `an` URL: https://languagetool.org/insights/post/indefinite-articles/ Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US Category: MISC
Raw output
resources/references/core-reference/dal-reference/fields-reference/index.md:30:40: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN)
 Suggestions: `an`
 URL: https://languagetool.org/insights/post/indefinite-articles/ 
 Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US
 Category: MISC
| 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

GitHub Actions / LanguageTool

[LanguageTool] resources/references/core-reference/dal-reference/fields-reference/index.md#L39

Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN) Suggestions: `an` URL: https://languagetool.org/insights/post/indefinite-articles/ Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US Category: MISC
Raw output
resources/references/core-reference/dal-reference/fields-reference/index.md:39:40: Use “an” instead of ‘a’ if the following word starts with a vowel sound, e.g. ‘an article’, ‘an hour’. (EN_A_VS_AN)
 Suggestions: `an`
 URL: https://languagetool.org/insights/post/indefinite-articles/ 
 Rule: https://community.languagetool.org/rule/show/EN_A_VS_AN?lang=en-US
 Category: MISC
| 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 |
4 changes: 4 additions & 0 deletions yarn.lock
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


0 comments on commit 744d71b

Please sign in to comment.