Skip to content

Commit a3c3af1

Browse files
committed
fix: handle boolean type in SafeValueConverter for CSV imports
SafeValueConverter::convertByDataType() had no handler for FieldDataType::BOOLEAN, causing string values like "true"/"false" from CSV imports to be inserted directly into the boolean_value column. MySQL strict mode rejects these string values for tinyint columns.
1 parent 0e6d8e7 commit a3c3af1

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/Support/SafeValueConverter.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public static function convertByDataType(mixed $value, FieldDataType $dataType):
5353
: self::toSafeInteger($value),
5454
FieldDataType::FLOAT => self::toSafeFloat($value),
5555
FieldDataType::MULTI_CHOICE => self::toSafeArray($value),
56+
FieldDataType::BOOLEAN => self::toSafeBoolean($value),
5657
default => $value,
5758
};
5859
}
@@ -133,6 +134,29 @@ public static function toSafeFloat(mixed $value): ?float
133134
return null;
134135
}
135136

137+
/**
138+
* Convert a value to a safe boolean (0 or 1) for database storage.
139+
*
140+
* @param mixed $value The value to convert
141+
* @return int|null The boolean value as 0/1 or null if empty
142+
*/
143+
public static function toSafeBoolean(mixed $value): ?int
144+
{
145+
if ($value === null || $value === '') {
146+
return null;
147+
}
148+
149+
if (is_bool($value)) {
150+
return $value ? 1 : 0;
151+
}
152+
153+
if (is_string($value)) {
154+
return in_array(mb_strtolower(trim($value)), ['true', '1', 'yes', 'on'], true) ? 1 : 0;
155+
}
156+
157+
return $value ? 1 : 0;
158+
}
159+
136160
/**
137161
* Convert a value to a safe string.
138162
*

0 commit comments

Comments
 (0)