Type casting for the Users_model.php

This commit is contained in:
Alex Tselegidis 2021-10-29 12:39:38 +02:00
parent d62dd9b053
commit b7305be038

View file

@ -19,6 +19,14 @@
* @package Models
*/
class Users_model extends EA_Model {
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'id_roles' => 'integer',
];
/**
* Save (insert or update) a user.
*
@ -171,6 +179,8 @@ class Users_model extends EA_Model {
$user = $this->db->get_where('users', ['id' => $user_id])->row_array();
$this->cast($user);
$user['settings'] = $this->db->get_where('user_settings', ['id_users' => $user_id])->row_array();
unset($user['settings']['id_users']);
@ -211,6 +221,8 @@ class Users_model extends EA_Model {
// Check if the required field is part of the user data.
$user = $query->row_array();
$this->cast($user);
if ( ! array_key_exists($field, $user))
{
throw new InvalidArgumentException('The requested field was not found in the user data: ' . $field);
@ -245,6 +257,8 @@ class Users_model extends EA_Model {
foreach ($users as &$user)
{
$this->cast($user);
$user['settings'] = $this->db->get_where('user_settings', ['id_users' => $user['id']])->row_array();
unset(
@ -343,7 +357,7 @@ class Users_model extends EA_Model {
*/
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{
return $this
$users = $this
->db
->select()
->from('users')
@ -362,6 +376,13 @@ class Users_model extends EA_Model {
->order_by($order_by)
->get()
->result_array();
foreach ($users as &$user)
{
$this->cast($user);
}
return $users;
}
/**