-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: [Model] Limitation in Boolean Support (true/false) in update()
/save()
/insert()
#9197
Comments
update()
/save()
/insert()
update()
/save()
/insert()
It seems |
If only PHPDoc is on the way to supporting booleans, then that's a good thing to change. I'm not a big fan of point 2. This would mean that each write query would be subject to an additional loop check of all passed arguments. Seems like a big waste of time/resources - for this one thing. If we want "magic" we have Entities or Data conversion in the model. When we use Query Builder we're responsible for the values we use/allow. If a certain database does not support booleans, it's not a framework's fault. |
@michalsn So, you prefer something similar to the code below and believe the user should adapt it for compatibility like this? // Hooks before insert and update
protected $beforeInsert = ['prepareDataForSQLiteAndOCI8'];
protected $beforeUpdate = ['prepareDataForSQLiteAndOCI8'];
/**
* Prepare data before saving or updating.
* This method adjusts 'is_pinned' field for specific database drivers like SQLite and OCI8.
*
* @param array $data The data being inserted or updated
* @return array Modified data
*/
private function prepareDataForSQLiteAndOCI8(array $data): array
{
// Get the database driver
$dbDriver = $this->db->DBDriver;
// Adjust 'is_pinned' for SQLite and OCI8 drivers
if (($dbDriver === 'SQLite3' || $dbDriver === 'OCI8') && isset($data['data']['is_pinned'])) {
$data['data']['is_pinned'] = $data['data']['is_pinned'] ? 1 : 0;
}
return $data;
} |
@datamweb I was thinking more about model field casting, although I don't remember if we have access to a database object there. |
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?
Currently, CodeIgniter 4 does not support boolean values (
true
andfalse
) in models due to restrictions in the PHPStan typing declaration (@phpstan-type row_array
).CodeIgniter4/system/BaseModel.php
Line 50 in 153922e
This definition only allows
int
,float
,null
,string
, andobject
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
TINYINT(1)
true
/false
(1/0)BOOLEAN
true
/false
BIT
true
/false
(1/0)INTEGER
NUMBER
Steps to Reproduce
To address this limitation and allow boolean values, it is suggested to:
true
andfalse
) for all supported databases.* @phpstan-type row_array array<int|string, float|int|null|object|string|bool>
true
to 1 andfalse
to 0.Example Code for Handling Boolean Conversion
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
The text was updated successfully, but these errors were encountered: