Backed Enums in CakePHP 4.6

I want to use backed enums in my CakePHP project like so:

enum ArticleStatus: int
{
    case DRAFT = 1;
    case PUBLISHED = 2;
} 
$article = $this->Articles->find()->where(['status' => ArticleStatus::DRAFT])->first();

However, Cake expects type int and not ArticleStatus. The same problem occurs when I try to patch an entity using an enum.

I understand that I could use the enum’s value property, but that seems somewhat verbose and error-prone. Is there a way for Cake to accept the enum type or automatically coerce it?

CakePHP 4 doesn’t support enums like you want it to. That has only been added to CakePHP 5.

1 Like