Skip to content

Custom Types

Convert between PHP-specific values and database storage values.

interface TypeConverterInterface
{
public function convertToPHP(mixed $value): mixed;
public function convertToDB(mixed $value): mixed;
}

Use a converter when the database representation is not the same shape as the PHP representation. Common examples are enums, value objects, booleans, dates, and geometry-like values.

Articulate ships built-in mappings — bool ↔ TINYINT(1), int ↔ INT, float ↔ FLOAT, string ↔ VARCHAR(255), DateTimeInterface ↔ DATETIME — plus named converters for common cases:

  • BoolTypeConverter
  • DateTimeTypeConverter
  • PointTypeConverter

Custom class mappings and TypeConverterInterface cover complex types, with priority-based resolution when a class implements multiple interfaces with registered mappings.

$typeRegistry->registerType('point', new PointTypeConverter());
$typeRegistry->registerClassMapping(Point::class, 'point');

Once registered, entity properties can use the type name through #[Property(type: 'point')] or class mapping.

public function setStatus(ProductStatus $status): void
{
$this->status = (new ProductStatusConverter())->convertToDatabase($status);
}
public function statusEnum(): ProductStatus
{
return (new ProductStatusConverter())->convertToPHP($this->status);
}