You can move column type definition to external class, or even combine it with accessor RecordAccessorInterface
. In order to do that, implement ColumnInterface
and write column type definition method.
interface ColumnInterface
{
/**
* Configure column.
*
* @param AbstractColumn $column
*/
public static function describeColumn(AbstractColumn $column);
}
ORM include base type for enum columns with associated accessor - EnumColumn
. Let's create column definition:
class UserStatus extends EnumColumn
{
const VALUES = ['active', 'blocked'];
const DEFAULT = 'active';
}
Use this column type in your model schema as normal type:
class User extends RecordEntity
{
const SCHEMA = [
'id' => 'primary',
'name' => 'string',
'status' => UserStatus::class
];
}
You can define your own method in such columns to be used as accessors later:
class UserStatus extends EnumColumn
{
const VALUES = ['active', 'blocked'];
const DEFAULT = 'active';
public function isBlocked(): bool
{
return $this->packValue() == 'blocked';
}
//...
}
if($user->status->isBlocked()) {
$user->status->setActive();
}