diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 5fa0b0ff78f..53de2f3d2ea 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -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()`. diff --git a/src/base/Field.php b/src/base/Field.php index d715627fe1a..d26e8c134b2 100644 --- a/src/base/Field.php +++ b/src/base/Field.php @@ -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; @@ -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 */ diff --git a/src/base/FieldInterface.php b/src/base/FieldInterface.php index 39c7631caeb..5a42a19dbc3 100644 --- a/src/base/FieldInterface.php +++ b/src/base/FieldInterface.php @@ -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; diff --git a/src/fields/Number.php b/src/fields/Number.php index 5f4ae2cbb77..b0b2f112cb2 100644 --- a/src/fields/Number.php +++ b/src/fields/Number.php @@ -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; } /** @@ -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 */