diff --git a/src/Persistence.php b/src/Persistence.php index b105c868a..58ec8bbac 100644 --- a/src/Persistence.php +++ b/src/Persistence.php @@ -400,12 +400,17 @@ private function _typecastPreField(Field $field, $value, bool $fromLoad) switch ($field->type) { case 'string': case 'text': + case 'boolean': + case 'smallint': case 'integer': + case 'bigint': case 'float': case 'decimal': case 'atk4_money': if (is_bool($value)) { - throw new Exception('Must not be bool type'); + if ($field->type !== 'boolean') { + throw new Exception('Must not be bool type'); + } } elseif (is_int($value)) { if ($fromLoad) { $value = (string) $value; diff --git a/tests/FieldTest.php b/tests/FieldTest.php index 33fd7066a..806a19554 100644 --- a/tests/FieldTest.php +++ b/tests/FieldTest.php @@ -680,6 +680,35 @@ public function testNormalizeStringArrayException(): void $m->set('foo', []); } + /** + * @dataProvider provideNormalizeNumericNonScalarExceptionCases + */ + #[DataProvider('provideNormalizeNumericNonScalarExceptionCases')] + public function testNormalizeNumericNonScalarException(string $type): void + { + $m = new Model(); + $m->addField('foo', ['type' => $type]); + $m = $m->createEntity(); + + $this->expectException(ValidationException::class); + $this->expectExceptionMessage('Must be scalar'); + $m->set('foo', []); + } + + /** + * @return iterable> + */ + public static function provideNormalizeNumericNonScalarExceptionCases(): iterable + { + yield ['boolean']; + yield ['smallint']; + yield ['integer']; + yield ['bigint']; + yield ['float']; + yield ['decimal']; + yield ['atk4_money']; + } + public function testNormalizeStringBoolException(): void { $m = new Model();