Skip to content
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

Add boolean field conversion #9146

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion system/BaseModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@
*/
protected $allowedFields = [];

/**
* An array of field names that should be cast to boolean.
*
* @var array<string>
*/
protected $booleanFields = [];

/**
* If true, will set created_at, and updated_at
* values during insert and update routines.
Expand Down Expand Up @@ -615,14 +622,59 @@
if ($this->tempAllowCallbacks) {
$eventData = $this->trigger('afterFind', $eventData);
}

$eventData = $this->convertBooleanFields($eventData);
$this->tempReturnType = $this->returnType;
$this->tempUseSoftDeletes = $this->useSoftDeletes;
$this->tempAllowCallbacks = $this->allowCallbacks;

return $eventData['data'];
}


/**
* Convert specific fields to boolean after fetching the data.
*
* @param array $data
* @return array
*/
protected function convertBooleanFields(array $data)

Check failure on line 640 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertBooleanFields() has parameter $data with no value type specified in iterable type array.

Check failure on line 640 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertBooleanFields() return type has no value type specified in iterable type array.
{
if (empty($this->booleanFields)) {

Check failure on line 642 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Construct empty() is not allowed. Use more strict comparison.
return $data;
}

if (isset($data['data'])) {
if (isset($data['data'][0])) {
// Multiple rows case
foreach ($data['data'] as &$row) {
$row = $this->convertFieldsToBoolean($row, $this->booleanFields);
}
} else {
// Single row case
$data['data'] = $this->convertFieldsToBoolean($data['data'], $this->booleanFields);
}
}

return $data;
}

/**
* Helper function to convert specific fields to boolean.
*
* @param array $data
* @param array $fields
* @return array
*/
private function convertFieldsToBoolean(array $data, array $fields)

Check failure on line 668 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertFieldsToBoolean() has parameter $data with no value type specified in iterable type array.

Check failure on line 668 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertFieldsToBoolean() has parameter $fields with no value type specified in iterable type array.

Check failure on line 668 in system/BaseModel.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis

Method CodeIgniter\BaseModel::convertFieldsToBoolean() return type has no value type specified in iterable type array.
{
foreach ($fields as $field) {
if (isset($data[$field])) {
$data[$field] = (bool) $data[$field];
}
}
return $data;
}

/**
* Fetches the column of database.
*
Expand Down
Loading