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:
1 parent
227325e4b4
commit
1478b8f7f7
2 files changed
+39
No files matched your search
@@ -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));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user