Type Casting

A C-style cast prefix coerces a property value to a specific PHP type. The cast is applied both in SQL via CAST() and in the hydrator, so the returned PHP value always matches the requested type regardless of driver behaviour.

explanation

Type Casting

Specify the target type directly in the retrieve list by prefixing a property with a cast:

$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve ((int)p.stock, (float)p.price, (string)p.code)
");

foreach ($results as $row) {
    $stock = $row['(int)p.stock']; // int
    $price = $row['(float)p.price']; // float
    $code  = $row['(string)p.code'];  // string
}

Use an explicit alias to give the result a clean key:

$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (stock = (int)p.stock, price = (float)p.price)
");

foreach ($results as $row) {
    $stock = $row['stock']; // int
    $price = $row['price']; // float
}

Supported cast types:

Cast keywordPHP typeSQL type (MySQL/MariaDB)
intintSIGNED
floatfloatDOUBLE
stringstringCHAR
decimalfloatDECIMAL
boolboolSIGNED
datetime\DateTimenone (PHP-only)

The datetime cast is PHP-only — no SQL CAST() is emitted. The raw database value (a Unix timestamp integer, a numeric string, or a "Y-m-d H:i:s" string) is converted to a \DateTime object in the hydrator. Unrecognisable values return null.

Casts can be used anywhere a property can appear: in where conditions, arithmetic expressions, and with explicit aliases:

// Cast in a where condition
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (p.name)
    where (int)p.stock > 0
");

// Cast with an explicit alias
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (price_float = (float)p.price)
");

// Cast on a JSON path property
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (stock = (int)p.attributes.stock)
");

// Cast as arithmetic operand
$results = $entityManager->executeQuery("
    range of p is App\\Entity\\ProductEntity
    retrieve (discounted = (float)p.price * 0.9)
");