Type Conversion

Nette Database automatically converts values retrieved from the database into the appropriate PHP types.

Date and Time

Date and time values are converted into Nette\Utils\DateTime objects. If you prefer date and time values to be converted into immutable Nette\Database\DateTime objects, enable the newDateTime option in the configuration.

$row = $database->fetch('SELECT created_at FROM articles');
echo $row->created_at instanceof DateTime; // true
echo $row->created_at->format('j. n. Y');

For MySQL, the TIME data type is converted into DateInterval objects.

Boolean Values

Boolean values are automatically normalized to true or false. In MySQL, the TINYINT(1) type is converted when the convertBoolean option is enabled in the configuration.

$row = $database->fetch('SELECT is_published FROM articles');
echo gettype($row->is_published); // 'boolean'

Numeric Values

Numeric values are cast to int or float, depending on the column type in the database:

$row = $database->fetch('SELECT id, price FROM products');
echo gettype($row->id);    // integer
echo gettype($row->price); // float

Custom Normalization

You can define a custom function to transform database rows using the setRowNormalizer(?callable $normalizer) method. This is useful, for example, for automatic data type conversion.

$database->setRowNormalizer(function(array $row, ResultSet $resultSet): array {
	// data type conversion happens here
	return $row;
});
version: 4.0 3.x