From 0e296138ff60788cc00692cd3db994d835d7332a Mon Sep 17 00:00:00 2001 From: twinh Date: Thu, 10 Nov 2022 15:30:43 +0800 Subject: [PATCH] =?UTF-8?q?feat(Model,=20Cast):=20`decimal`=20=E5=AD=97?= =?UTF-8?q?=E6=AE=B5=EF=BC=8C=E5=A6=82=E6=9E=9C=E4=BC=A0=E5=85=A5=E7=A9=BA?= =?UTF-8?q?=E5=80=BC=EF=BC=8C=E8=87=AA=E5=8A=A8=E8=BD=AC=E6=8D=A2=E4=B8=BA?= =?UTF-8?q?=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 如果传入空值,例如划线价允许为空,会出现错误: Incorrect decimal value: '' for column 'xxx' at row 1 --- lib/Model/CastTrait.php | 4 +++- tests/unit/Model/CastTraitTest.php | 33 +++++++++++++++++++++++++++ tests/unit/Model/Fixture/TestCast.php | 2 ++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/lib/Model/CastTrait.php b/lib/Model/CastTrait.php index b3d526b9a..2ca271113 100644 --- a/lib/Model/CastTrait.php +++ b/lib/Model/CastTrait.php @@ -194,9 +194,11 @@ protected function castValueToDb($value, $type) case 'string': case 'float': - case 'decimal': return (string) $value; + case 'decimal': + return (string) $value ?: 0; + case 'date': case 'datetime': // Note that default value of the date* column is null, so they are always nullable diff --git a/tests/unit/Model/CastTraitTest.php b/tests/unit/Model/CastTraitTest.php index aa0e2b13f..6b5fe994b 100644 --- a/tests/unit/Model/CastTraitTest.php +++ b/tests/unit/Model/CastTraitTest.php @@ -44,6 +44,8 @@ public static function setUpBeforeClass(): void ->string('list_column') ->string('nullable_list_column')->nullable() ->string('list2_column') + ->decimal('decimal_column') + ->decimal('nullable_decimal_column')->nullable() ->exec(); wei()->db->batchInsert('test_casts', [ @@ -580,6 +582,8 @@ public function testSetNull() 'list_column' => '', 'nullable_list_column' => null, 'list2_column' => '', + 'decimal_column' => '0.00', + 'nullable_decimal_column' => null, ], $data); } @@ -637,6 +641,8 @@ public function testGetColumnCasts() 'type' => 'int', 'separator' => '|', ], + 'decimal_column' => 'decimal', + 'nullable_decimal_column' => 'decimal', ], $casts); } @@ -688,6 +694,8 @@ public function testDefaultToArray() 'list_column' => [], 'nullable_list_column' => null, 'list2_column' => [], + 'decimal_column' => '0', + 'nullable_decimal_column' => null, ], $array); } @@ -779,4 +787,29 @@ public function testIntString() $this->assertSame('', $cast->big_int_column); $this->assertSame('1', $cast->nullable_big_int_column); } + + /** + * @dataProvider providerForTestSaveDecimal + * @param mixed $value + * @param mixed $decimalValue + * @param mixed $nullableDecimalValue + */ + public function testSaveDecimal($value, $decimalValue, $nullableDecimalValue) + { + $cast = TestCast::save([ + 'decimal_column' => $value, + 'nullable_decimal_column' => $value, + ]); + $this->assertSame($decimalValue, $cast->decimal_column); + $this->assertSame($nullableDecimalValue, $cast->nullable_decimal_column); + } + + public function providerForTestSaveDecimal(): array + { + return [ + ['', '0', '0'], + [null, '0', null], + [false, '0', '0'], + ]; + } } diff --git a/tests/unit/Model/Fixture/TestCast.php b/tests/unit/Model/Fixture/TestCast.php index 9cdf3d341..a18e47615 100644 --- a/tests/unit/Model/Fixture/TestCast.php +++ b/tests/unit/Model/Fixture/TestCast.php @@ -23,6 +23,8 @@ * @property object $default_object_column * @property array $list_column * @property array $list2_column + * @property string $decimal_column + * @property string|null $nullable_decimal_column */ class TestCast extends BaseModel {