Skip to content

Commit

Permalink
Improved scalar Number field value formats
Browse files Browse the repository at this point in the history
Resolves #16369
  • Loading branch information
brandonkelly committed Jan 2, 2025
1 parent 8b1c4d7 commit 5eeb785
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG-WIP.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,14 @@
- The `duration` Twig filter now has a `language` argument. ([#16332](https://github.com/craftcms/cms/pull/16332))
- The `indexOf` Twig filter now has a `default` argument, which can be any integer or `null`. (`-1` by default for backwards compatibility.)
- It’s now possible to reference custom field handles in element queries’ `where` params. ([#16318](https://github.com/craftcms/cms/pull/16318))
- Number fields’ scalar values now return an integer if Decimals is set to `0`, and a number formatted with the correct decimal points when using MySQL. ([16369](https://github.com/craftcms/cms/issues/16369))
- Deprecated the `ucfirst` Twig filter. `capitalize` should be used instead.

### Extensibility
- Added `craft\base\Element::EVENT_DEFINE_ALT_ACTIONS`. ([#16294](https://github.com/craftcms/cms/pull/16294))
- Added `craft\base\ElementInterface::getAltActions()`. ([#16294](https://github.com/craftcms/cms/pull/16294))
- Added `craft\base\ElementTrait::$viewMode`. ([#16324](https://github.com/craftcms/cms/pull/16324))
- Added `craft\base\Field::dbTypeForValueSql()`.
- Added `craft\base\NestedElementTrait::ownerType()`.
- Added `craft\base\conditions\BaseElementSelectConditionRule::allowMultiple()`.
- Added `craft\base\conditions\BaseElementSelectConditionRule::getElementIds()`.
Expand Down
14 changes: 13 additions & 1 deletion src/base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -968,7 +968,7 @@ public function getValueSql(?string $key = null): ?string

private function _valueSql(?string $key): ?string
{
$dbType = static::dbType();
$dbType = $this->dbTypeForValueSql();

if ($dbType === null) {
return null;
Expand Down Expand Up @@ -1043,6 +1043,18 @@ private function _valueSql(?string $key): ?string
return $sql;
}

/**
* Returns the DB data type(s) that this field will store within the `elements_sites.content` column.
*
* @see dbType()
* @return string|string[]|null The data type(s).
* @since 5.6.0
*/
protected function dbTypeForValueSql(): array|string|null
{
return static::dbType();
}

/**
* @inheritdoc
*/
Expand Down
2 changes: 1 addition & 1 deletion src/base/FieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static function supportedTranslationMethods(): array;
public static function phpType(): string;

/**
* Returns the DB data type(s) that this field will store within the `elements_sites.content` column.
* Returns the DB data type(s) that fields of this type will store within the `elements_sites.content` column.
*
* ```php
* return \yii\db\Schema::TYPE_STRING;
Expand Down
23 changes: 21 additions & 2 deletions src/fields/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,11 @@ public static function phpType(): string
*/
public static function dbType(): string
{
$db = Craft::$app->getDb();
return $db->getIsMysql() ? sprintf('%s(65,16)', Schema::TYPE_DECIMAL) : Schema::TYPE_DECIMAL;
if (Craft::$app->getDb()->getIsMysql()) {
return sprintf('%s(65,16)', Schema::TYPE_DECIMAL);
}

return Schema::TYPE_DECIMAL;
}

/**
Expand Down Expand Up @@ -325,6 +328,22 @@ public function getElementConditionRuleType(): array|string|null
return NumberFieldConditionRule::class;
}

/**
* @inheritdoc
*/
protected function dbTypeForValueSql(): array|string|null
{
if (!$this->decimals) {
return Schema::TYPE_INTEGER;
}

if (Craft::$app->getDb()->getIsMysql()) {
return sprintf('%s(65,%s)', Schema::TYPE_DECIMAL, $this->decimals);
}

return Schema::TYPE_DECIMAL;
}

/**
* @inheritdoc
*/
Expand Down

0 comments on commit 5eeb785

Please sign in to comment.