The enum field casts are not behaving as intended and are resulting in an error related to conversion to a string.
agrism opened this issue · 2 comments
agrism commented
The enum field casts are not functioning as expected.
Error found: Object of class Enum could not be converted to string
PHP version: 8.1
Laravel version: 8.83.27
If you'd like to reproduce the error, follow these steps.
- Create a Model:
namespace App\Models;
use Alvin0\RedisModel\Model;
use App\Enums\StatusEnum;
/**
* @property string $id
* @property StatusEnum $status
*/
class MyEnumModel extends Model
{
protected $primaryKey = 'id';
protected $subKeys = [
'status',
];
protected $fillable = [
'status',
];
protected $casts = [
'status' => StatusEnum::class
];
public $incrementing = true;
protected $table = "my_enum_models";
}
- Create an Enum:
namespace App\Enums;
enum StatusEnum: string
{
case NEW = 'new';
case FAILED = 'failed';
case COMPLETED = 'completed';
}
3 Test the model:
$notifyIfStatusNotEnum = function(\App\Models\MyEnumModel $model): void
{
if(!$model->status instanceof \App\Enums\StatusEnum){
dump('status not Enum instance');
}
};
$model = new \App\Models\MyEnumModel;
$model->status = \App\Enums\StatusEnum::NEW;
$notifyIfStatusNotEnum($model);
$model->save(); // -> Error: `Object of class App\Enums\StatusEnum could not be converted to string`