fix: convert boolean to tinyint

This commit is contained in:
Victor Westerlund 2024-01-08 14:24:50 +01:00
parent 4ffa2ee24f
commit ba8e69e7a3

View file

@ -39,6 +39,16 @@
return array_values(is_array($input) ? $input : [$input]); return array_values(is_array($input) ? $input : [$input]);
} }
// Convert value to MySQL tinyint
private static function filter_boolean(mixed $value): int {
return (int) filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
// Convert all boolean type values to tinyints in array
private static function filter_booleans(array $values): array {
return array_map(fn($v): mixed => gettype($v) === "boolean" ? self::filter_boolean($v) : $v, $values);
}
// Return value(s) that exist in $this->model // Return value(s) that exist in $this->model
private function in_model(string|array $columns): ?array { private function in_model(string|array $columns): ?array {
// Place string into array // Place string into array
@ -226,8 +236,10 @@
// Get array of SQL WHERE string and filter values // Get array of SQL WHERE string and filter values
$filter_sql = !is_null($this->filter_sql) ? " WHERE {$this->filter_sql}" : ""; $filter_sql = !is_null($this->filter_sql) ? " WHERE {$this->filter_sql}" : "";
$values = array_values($entity); // Get values from entity and convert booleans to tinyint
// Append filter values if defined $values = self::filter_booleans(array_values($entity));
// Append values to filter property if where() was chained
if ($this->filter_values) { if ($this->filter_values) {
array_push($values, ...$this->filter_values); array_push($values, ...$this->filter_values);
} }
@ -246,6 +258,9 @@
throw new Exception("Values length does not match columns in model"); throw new Exception("Values length does not match columns in model");
} }
// Convert booleans to tinyint
$values = self::filter_booleans($values);
// Create CSV string with Prepared Statement abbreviatons from length of fields array. // Create CSV string with Prepared Statement abbreviatons from length of fields array.
$values_stmt = implode(",", array_fill(0, count($values), "?")); $values_stmt = implode(",", array_fill(0, count($values), "?"));