Skip to content

bug(mysql): DataType::BOOL columns fail on insert/update — raw PHP booleans not cast to int for bit(1) #135

@usernane

Description

@usernane

Bug Description

DataType::BOOL columns map to bit(1) in MySQL. The prepared statement binding in MySQLQuery::addBinding() and MySQLInsertBuilder::parseValues() passes the raw PHP boolean without casting to integer. MySQL's bit(1) rejects this with "Data too long for column".

Steps to Reproduce

Define a BOOL column:

#[Column(name: 'is_active', type: DataType::BOOL, default: true)]

Insert with a PHP boolean:

$db->table('users')->insert(['is-active' => true])->execute();
// Error: 1406 - Data too long for column 'is_active'

Root Cause

In MySQLQuery::addBinding() (line 44):

$this->bindings['values'][] = $value;  // Raw PHP true/false

In MySQLInsertBuilder::parseValues() (line 36):

$queryParams['values'][$index][] = $val;  // Raw PHP true/false

Both set bind type to 'i' (integer) but don't cast the value. When mysqli_stmt::bind_param() receives a PHP boolean with type 'i', it doesn't convert it to 1/0 properly for bit(1) columns.

Note: MySQLColumn::cleanValueHelper() correctly handles this for raw SQL by converting to b'1'/b'0', but the prepared statement path bypasses cleanValue().

Proposed Fix

Cast the value to (int) before adding to bindings in both locations:

MySQLQuery::addBinding():

if ($colType == 'int' || $colType == 'bit' || in_array($colType, Column::BOOL_TYPES)) {
    $this->bindings['values'][] = (int) $value;
    $this->bindings['bind'] .= 'i';
}

MySQLInsertBuilder::parseValues():

if ($colType == 'int' || $colType == 'bit' || in_array($colType, Column::BOOL_TYPES)) {
    $queryParams['bind'] .= 'i';
    $queryParams['values'][$index][] = (int) $val;
}

WebFiori Version

webfiori/database v2.0.3

PHP Version

8.4.18

Operating System

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions