Add helper method for optional fields
This commit is contained in:
parent
d19e8a3f84
commit
d2c6d5a571
1 changed files with 25 additions and 0 deletions
|
@ -187,6 +187,31 @@ class EA_Model extends CI_Model {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure a field exists in an array by using its value or NULL.
|
||||
*
|
||||
* @param array $record Record data (single or multiple records).
|
||||
* @param array $fields Requested field names.
|
||||
*/
|
||||
public function optional(array &$record, array $fields)
|
||||
{
|
||||
$sanitize = function ($field) use (&$record) {
|
||||
return $record[$field] ?? NULL;
|
||||
};
|
||||
|
||||
if (is_assoc($record))
|
||||
{
|
||||
$record = array_map($sanitize, $fields);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach ($record as &$record_item)
|
||||
{
|
||||
$record_item = array_map($sanitize, $fields);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the DB field name based on an API field name.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue