Welcome Guest, Not a member yet? Register   Sign In
Need refactoring advice
#1

Hello, all.

Here is my model superclass (used by several child models):

PHP Code:
<?php

class MY_Model extends CI_Model
{

    protected 
$table '';

    protected function 
switch_db($webinar_id)
    {
        if (! 
$this->db->db_select('webinar_' $webinar_id)) die('Вебинар не существует');
    }

    public function 
get($webinar_id$id 0$filter = [])
    {
        
$this->switch_db($webinar_id);
        
$where_clause = ($id == ? [] : ['id' => $id]) + $filter;
        
$query $this->db->get_where($this->table$where_clause);
        return (
$id == $query->result_array() : $query->row_array());
    }

    public function 
get_only_status($webinar_id$id 0$filter = [])
    {
        
$this->switch_db($webinar_id);
        
$where_clause = ($id == ? [] : ['id' => $id]) + $filter;
        
$query $this->db->select('id, status')->get_where($this->table$where_clause);
        return (
$id == $query->result_array() : $query->row_array());
    }

    public function 
add($webinar_id$data)
    {
        
$this->switch_db($webinar_id);
        
$data['status'] = 0;
        
$data['date'] = date('Y-m-d H:i:s');
        
$this->db->insert($this->table$data);
    }

    public function 
delete($webinar_id$id)
    {
        
$this->switch_db($webinar_id);
        
$this->db->delete($this->table, ['id' => $id]);
    }

    public function 
edit($webinar_id$id$data)
    {
        
$this->switch_db($webinar_id);
        
$data['date'] = date('Y-m-d H:i:s');
        
$this->db->update($this->table$data, ['id' => $id]);
    }

    public function 
update_status($webinar_id$data)
    {
        
$this->switch_db($webinar_id);
        if (
$data['status'] == 1)
        {
            
$tool $this->get($webinar_id$data['tool_id']);
            if (! empty(
$tool)) foreach (['banners','timers','polls','randoms','subscriptions'] as $table)
                
$this->db->update($table, ['status' => 0], ['block_id' => $tool['block_id'],'status' => 1]);
        }
        
$this->db->update($this->table, ['status' => $data['status']], ['id' => $data['tool_id']]);
    }


You see this $this->switch_db($webinar_id); at the beginning of each method? It is used because of a separate db for each webinar (conference) instance. The problem that i need to pass $webinar_id param every time, which i get in controller as part of a URL. Duplicating code looks a bit awkward. I thought about constructor - BUT i can't pass an argument to it. Undecided Any suggestions?

Please, don't argue against "multi-db" decision - it can't be undone and has several advantages (smaller databases, better backup strategy).
Reply


Messages In This Thread
Need refactoring advice - by Rashid - 10-15-2015, 06:53 AM
RE: Need refactoring advice - by mwhitney - 10-15-2015, 08:12 AM
RE: Need refactoring advice - by Rashid - 10-16-2015, 01:37 AM
RE: Need refactoring advice - by mwhitney - 10-16-2015, 07:54 AM



Theme © iAndrew 2016 - Forum software by © MyBB