Enum.md 1.7 KB

EnumTrait

Enum support via trait and enum() method.

Add the trait to your entity:

use Tools\Model\Entity\EnumTrait;

class MyEntity extends Entity {

    use EnumTrait;

Then add your enums like so:

    /**
     * @param int|array|null $value
     *
     * @return array|string
     */
    public static function statuses($value = null) {
        $options = [
            static::STATUS_PENDING => __('Pending'),
            static::STATUS_SUCCESS => __('Success'),
            static::STATUS_FAILURE => __('Failure'),
        ];
        return parent::enum($value, $options);
    }

    public const STATUS_PENDING = 0;
    public const STATUS_SUCCESS = 1;
    public const STATUS_FAILURE = 2;    

You can now use it in the forms in your templates:

<?= $this->Form->create($entity) ?>
...
<?= $this->Form->control('status', ['options' => $entity::statuses()]) ?>

And in your index or view:

echo $entity::statuses($entity->status);

Make sure the property is not null (or it would return an array). Best to check for it before or combine it with Shim plugin GetTrait and $entity->getStatusOrFail():

// Allowed to be empty
echo $entity->status !== null ? $entity::statuses($entity->status) : $default;

// Required or throw exception
echo $entity::statuses($entity->getStatusOrFail());

Bake template support

Use the Setup plugin (--theme Setup) to get auto-support for your templates based on the existing enums you added.

The above form controls would be auto-added by this.

Background

See Static Enums.