Type casting for the Customers_model.php

This commit is contained in:
Alex Tselegidis 2021-10-29 12:38:56 +02:00
parent 6cc17d7bf5
commit 9338ec4445

View file

@ -19,6 +19,14 @@
* @package Models * @package Models
*/ */
class Customers_model extends EA_Model { class Customers_model extends EA_Model {
/**
* @var array
*/
protected $casts = [
'id' => 'integer',
'id_roles' => 'integer',
];
/** /**
* Save (insert or update) a customer. * Save (insert or update) a customer.
* *
@ -177,7 +185,11 @@ class Customers_model extends EA_Model {
throw new InvalidArgumentException('The provided customer ID was not found in the database: ' . $customer_id); throw new InvalidArgumentException('The provided customer ID was not found in the database: ' . $customer_id);
} }
return $this->db->get_where('users', ['id' => $customer_id])->row_array(); $customer = $this->db->get_where('users', ['id' => $customer_id])->row_array();
$this->cast($customer);
return $customer;
} }
/** /**
@ -213,6 +225,8 @@ class Customers_model extends EA_Model {
// Check if the required field is part of the customer data. // Check if the required field is part of the customer data.
$customer = $query->row_array(); $customer = $query->row_array();
$this->cast($customer);
if ( ! array_key_exists($field, $customer)) if ( ! array_key_exists($field, $customer))
{ {
throw new InvalidArgumentException('The requested field was not found in the customer data: ' . $field); throw new InvalidArgumentException('The requested field was not found in the customer data: ' . $field);
@ -245,7 +259,14 @@ class Customers_model extends EA_Model {
$this->db->order_by($order_by); $this->db->order_by($order_by);
} }
return $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array(); $customers = $this->db->get_where('users', ['id_roles' => $role_id], $limit, $offset)->result_array();
foreach ($customers as &$customer)
{
$this->cast($customer);
}
return $customers;
} }
/** /**
@ -353,8 +374,8 @@ class Customers_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_customer_role_id(); $role_id = $this->get_customer_role_id();
return $this $customers = $this
->db ->db
->select() ->select()
->from('users') ->from('users')
@ -374,6 +395,13 @@ class Customers_model extends EA_Model {
->order_by($order_by) ->order_by($order_by)
->get() ->get()
->result_array(); ->result_array();
foreach ($customers as $customer)
{
$this->cast($customer);
}
return $customers;
} }
/** /**