fix(typecaster): send booleans to the driver as 1/0
Tests / PHP 8.4 (push) Failing after 5m57s

PDO with ATTR_EMULATE_PREPARES = false sends a native PHP bool as an empty
string. MariaDB rejects it on a TINYINT column with "Incorrect integer
value: ''", so every boolean column was unwritable through the ORM:
inserting a model with a bool property always failed.

toDatabaseValue() now maps bool to 1/0. Other scalars, enums, dates and
BigDecimal keep their existing handling.

Covered by TypeCasterDatabaseValueTest.
This commit is contained in:
dgarcia committed 2026-11-15 14:21:40 -03:00
1 parent 227325e4b4
commit 1478b8f7f7
2 files changed
+39

No files matched your search

+6
View File
@@ -116,6 +116,12 @@ final class TypeCaster
return $value->format('Y-m-d H:i:s'); return $value->format('Y-m-d H:i:s');
} }
// PDO with ATTR_EMULATE_PREPARES = false sends a native bool as an empty string, which a
// TINYINT rejects with "Incorrect integer value: ''". Boolean columns are always 0/1.
if (is_bool($value)) {
return $value ? 1 : 0;
}
return $value; return $value;
} }
@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Camelia\Orm\Tests\Unit\Support;
use Camelia\Orm\Support\TypeCaster;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
/**
* Regression: PDO with ATTR_EMULATE_PREPARES = false sends a native PHP bool as an empty string.
* MySQL/MariaDB answers "Incorrect integer value: '' for column ...", so every TINYINT column was
* unwritable. Booleans must reach the driver as 1/0.
*/
final class TypeCasterDatabaseValueTest extends TestCase
{
#[Test]
public function it_converts_a_bool_to_an_integer(): void
{
self::assertSame(1, TypeCaster::toDatabaseValue(true));
self::assertSame(0, TypeCaster::toDatabaseValue(false));
}
#[Test]
public function it_leaves_other_scalars_untouched(): void
{
self::assertSame(42, TypeCaster::toDatabaseValue(42));
self::assertSame('texto', TypeCaster::toDatabaseValue('texto'));
self::assertSame(1.5, TypeCaster::toDatabaseValue(1.5));
self::assertNull(TypeCaster::toDatabaseValue(null));
}
}