@@ -493,7 +493,7 @@ To use this type you need to specify which column is associated to which BackedE
493
493
$this->getSchema()->setColumnType('status', EnumType::from(ArticleStatus::class));
494
494
}
495
495
496
- Where ``ArticleStatus `` contains something like::
496
+ A simple ``ArticleStatus `` could look like::
497
497
498
498
namespace App\Model\Enum;
499
499
@@ -503,6 +503,36 @@ Where ``ArticleStatus`` contains something like::
503
503
case Unpublished = 'N';
504
504
}
505
505
506
+ CakePHP also provides the ``EnumLabelInterface `` which can be implemented by
507
+ Enums that want to provide a map of human-readable labels::
508
+
509
+ namespace App\Model\Enum;
510
+
511
+ use Cake\Database\Type\EnumLabelInterface;
512
+
513
+ enum ArticleStatus: string implements EnumLabelInterface
514
+ {
515
+ case Published = 'Y';
516
+ case Unpublished = 'N';
517
+
518
+ public static function label(): string
519
+ {
520
+ return match ($this) {
521
+ self::Published->value => __('Published'),
522
+ self::Unpublished->value => __('Unpublished'),
523
+ };
524
+ }
525
+ }
526
+
527
+ This can be useful if you want to use your enums in ``FormHelper `` select
528
+ inputs. You can use `bake </bake >`_ to generate an enum class::
529
+
530
+ # generate an enum class with two cases and stored as an integer
531
+ bin/cake bake enum UserStatus inactive:0,active:1 -i
532
+
533
+ # generate an enum class with two cases as a string
534
+ bin/cake bake enum UserStatus published:Y,unpublished:N
535
+
506
536
CakePHP recommends a few conventions for enums:
507
537
508
538
- Enum classnames should follow ``{Entity}{ColumnName} `` style to enable
@@ -1100,7 +1130,7 @@ databases. For example to create a database::
1100
1130
.. note ::
1101
1131
1102
1132
When creating a database it is a good idea to set the character set and
1103
- collation parameters (e.g. ``DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci ``).
1133
+ collation parameters (e.g. ``DEFAULT CHARACTER SET utf8mb4 DEFAULT COLLATE utf8mb4_unicode_ci ``).
1104
1134
If these values are missing, the database will set whatever system default values it uses.
1105
1135
1106
1136
.. meta ::
0 commit comments