read_replica = $this->load->database('savvy_replica', TRUE); } public function get_carpool_friend_records( $filters = [], $limit = null, $offset = null ) { $combo_array = [ 'proc_status' => 'cf.status', 'status' => 'cf.status' ]; $numeric_array = [ 'member_id' => 'cp.member_id', 'carpool_id' => 'cf.carpool_id' ]; $this->read_replica->select([ '(CASE WHEN cf.status = 1 THEN \'Pending\' WHEN cf.status = 4 THEN \'Accepted\' ELSE cf.status::text END ) AS proc_status', '\'\'||m.firstname||\'\' AS Member', 'm.id AS member_id', 'cf.*' ]); $this->read_replica->from('members_carpool_friends cf'); $this->read_replica->join('members_carpool cp', 'cp.id=cf.carpool_id', 'left'); $this->read_replica->join('members m', 'm.id = cp.member_id', 'left'); foreach($filters as $key => $val) { if (array_key_exists($key, $combo_array)) { $this->read_replica->where($combo_array[$key], $val); } else if (array_key_exists($key, $numeric_array)) { $this->read_replica->where($key, $val); } else if (strpos($key, 'from') !== FALSE) { $this->read_replica->where('DATE(cf.added) >=', $val); } else if (strpos($key, 'to') !== FALSE) { $this->read_replica->where('DATE(cf.added) <=', $val); } else { $this->read_replica->like('lower(cf.' . $key . ')', strtolower($val)); } } if ($limit) { $this->read_replica->limit($limit, $offset); } return $this->read_replica->get()->result_array(); } }