Type casting for the Admins_model.php

This commit is contained in:
Alex Tselegidis 2021-10-29 12:38:41 +02:00
parent 64205cb23b
commit d1b78397ef
1 changed files with 34 additions and 5 deletions

View File

@ -19,6 +19,14 @@
* @package Models * @package Models
*/ */
class Admins_model extends EA_Model { class Admins_model extends EA_Model {
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'id_roles' => 'integer',
];
/** /**
* Save (insert or update) an admin. * Save (insert or update) an admin.
* *
@ -261,6 +269,8 @@ class Admins_model extends EA_Model {
$admin = $this->db->get_where('users', ['id' => $admin_id])->row_array(); $admin = $this->db->get_where('users', ['id' => $admin_id])->row_array();
$this->cast($admin);
$admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin_id])->row_array(); $admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin_id])->row_array();
unset( unset(
@ -305,6 +315,8 @@ class Admins_model extends EA_Model {
// Check if the required field is part of the admin data. // Check if the required field is part of the admin data.
$admin = $query->row_array(); $admin = $query->row_array();
$this->cast($admin);
if ( ! array_key_exists($field, $admin)) if ( ! array_key_exists($field, $admin))
{ {
throw new InvalidArgumentException('The requested field was not found in the admin data: ' . $field); throw new InvalidArgumentException('The requested field was not found in the admin data: ' . $field);
@ -341,6 +353,8 @@ class Admins_model extends EA_Model {
foreach ($admins as &$admin) foreach ($admins as &$admin)
{ {
$this->cast($admin);
$admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array(); $admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array();
unset( unset(
@ -441,8 +455,8 @@ class Admins_model extends EA_Model {
*/ */
public function query(): CI_DB_query_builder public function query(): CI_DB_query_builder
{ {
$role_id = $this->get_admin_role_id(); $role_id = $this->get_admin_role_id();
return $this->db->from('users')->where('id_roles', $role_id); return $this->db->from('users')->where('id_roles', $role_id);
} }
@ -458,9 +472,9 @@ class Admins_model extends EA_Model {
*/ */
public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array public function search(string $keyword, int $limit = NULL, int $offset = NULL, string $order_by = NULL): array
{ {
$role_id = $this->get_admin_role_id(); $role_id = $this->get_admin_role_id();
return $this $admins = $this
->db ->db
->select() ->select()
->from('users') ->from('users')
@ -480,6 +494,21 @@ class Admins_model extends EA_Model {
->order_by($order_by) ->order_by($order_by)
->get() ->get()
->result_array(); ->result_array();
foreach ($admins as &$admin)
{
$this->cast($admin);
$admin['settings'] = $this->db->get_where('user_settings', ['id_users' => $admin['id']])->row_array();
unset(
$admin['settings']['id_users'],
$admin['settings']['password'],
$admin['settings']['salt']
);
}
return $admins;
} }
/** /**