-
Notifications
You must be signed in to change notification settings - Fork 2k
Description
PHP Version
8.3
CodeIgniter4 Version
v4.5.4
CodeIgniter4 Installation Method
Composer (using codeigniter4/appstarter)
Which operating systems have you tested for this bug?
Windows
Which server did you use?
apache
Database
No response
What happened?
$this->update($messageId, [
'is_pinned' => false,
]); ------ ------------------------------------------------------------------------------------------------------
Line src\Models\MessageModel.php
------ ------------------------------------------------------------------------------------------------------
107 Parameter #2 $row of method Datamweb\BlankPackage\Models\BaseModel::update() expects array<int|string,
float|int|object|string|null>|object|null, array<string, int|false|null> given.
------ ------------------------------------------------------------------------------------------------------Currently, CodeIgniter 4 does not support boolean values (true and false) in models due to restrictions in the PHPStan typing declaration (@phpstan-type row_array).
CodeIgniter4/system/BaseModel.php
Line 50 in 153922e
| * @phpstan-type row_array array<int|string, float|int|null|object|string> |
This definition only allows
int, float, null, string, and object types, effectively preventing boolean values from being used in database operations.
This issue is particularly problematic because certain database drivers natively support boolean values, and they should be allowed for use directly.
Database Driver Details
| Database | Boolean Support | Data Type | Accepted Values |
|---|---|---|---|
| MySQL | Yes | TINYINT(1) |
true / false (1/0) |
| PostgreSQL | Yes | BOOLEAN |
true / false |
| SQL Server | Yes | BIT |
true / false (1/0) |
| SQLite | No (requires conversion) | INTEGER |
1 / 0 |
| OCI8 (Oracle) | No (requires conversion) | NUMBER |
1 / 0 |
Steps to Reproduce
To address this limitation and allow boolean values, it is suggested to:
- Update the PHPStan-type declaration to support boolean values (
trueandfalse) for all supported databases.
* @phpstan-type row_array array<int|string, float|int|null|object|string|bool> - Add automatic conversion for databases that do not support boolean values directly (like SQLite and OCI8) by converting
trueto 1 andfalseto 0.
Example Code for Handling Boolean Conversion
protected function prepareDataForSQLiteAndOCI8(array $data): array
{
$dbDriver = $this->db->DBDriver;
if ($dbDriver === 'SQLite3' || $dbDriver === 'OCI8') {
foreach ($data as $key => $value) {
if (is_bool($value)) {
$data[$key] = $value ? 1 : 0;
}
}
}
return $data;
}These changes will ensure better compatibility with all database drivers, allowing proper handling of boolean values in databases that do not natively support them.
Expected Output
Manually casting boolean values is a workaround and places unnecessary burden on developers. CodeIgniter 4 should handle these conversions internally, especially given that:
Many database drivers natively support boolean values (true and false).
The framework should abstract away such differences, allowing developers to focus on business logic rather than database-specific nuances.
In conclusion, while manual casting (e.g., (int)false OR 1/0) solves the problem for now, this limitation should not exist, as proper support for boolean values will make the framework more robust and user-friendly.
Anything else?
No response