Skip to content

Commit

Permalink
feat(Model, Cast): decimal 字段,如果传入空值,自动转换为 0
Browse files Browse the repository at this point in the history
如果传入空值,例如划线价允许为空,会出现错误:
 Incorrect decimal value: '' for column 'xxx' at row 1
  • Loading branch information
twinh committed Nov 22, 2022
1 parent f38dc31 commit 0e29613
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Model/CastTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 33 additions & 0 deletions tests/unit/Model/CastTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', [
Expand Down Expand Up @@ -580,6 +582,8 @@ public function testSetNull()
'list_column' => '',
'nullable_list_column' => null,
'list2_column' => '',
'decimal_column' => '0.00',
'nullable_decimal_column' => null,
], $data);
}

Expand Down Expand Up @@ -637,6 +641,8 @@ public function testGetColumnCasts()
'type' => 'int',
'separator' => '|',
],
'decimal_column' => 'decimal',
'nullable_decimal_column' => 'decimal',
], $casts);
}

Expand Down Expand Up @@ -688,6 +694,8 @@ public function testDefaultToArray()
'list_column' => [],
'nullable_list_column' => null,
'list2_column' => [],
'decimal_column' => '0',
'nullable_decimal_column' => null,
], $array);
}

Expand Down Expand Up @@ -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'],
];
}
}
2 changes: 2 additions & 0 deletions tests/unit/Model/Fixture/TestCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down

0 comments on commit 0e29613

Please sign in to comment.