Welcome Guest, Not a member yet? Register   Sign In
DB obj passed from controller to model
#1

[eluser]Rahi[/eluser]
Hi,

I am encountering a situation where I have two database objs created in controller constructor. Now when I load model I have to pass database objs to model so that I don't have to create db instance again in the model. My code will surely clear the situation what I am talking about.

Controller

Code:
class Team_Users extends Controller {
    public $sourceDB = '';
    public $destinationDB='';
    
    function __construct(){
        parent:: __construct();
        $this->sourceDB = $this->load->database("db1", TRUE);
        $this->destinationDB = $this->load->database("db2", TRUE);
        $this->load->model("team_users_model", "", $this->destinationDB);
    }
}

Model

Code:
class Team_Users_Model extends MY_Model {

    function __construct() {

        parent::__construct();

// Sets data of table associated to the model

        $this->setTableData('team_users');

// Sets validation rules for the model

        $this->setValidation(
            array(
                    'first_name' => 'required',
                    'last_name' => 'required',
                    'user_name' => 'required',
                    'password' => 'required',
                    'email' => 'required|valid_email'
                 )
            );

    }

}

And of course the extended model which is throwing error

MY_Model

Code:
abstract class MY_Model extends Model {

    protected $table = ''; // table associated to the model

    protected $fields = array(); // fields of table associated to the model

        protected $errors = array(); // model errors

    protected $tablePrefix = "ih_";



    /**

     * Constructor

     * @access protected

     */

    protected function __construct() {

        parent::Model();

// get CI superobject as a model property

        $this->ci =& get_instance();

    }



    public function setTableData($table = 'default') {

        if ($this->db->table_exists($table)) {

            $this->table = $this->tablePrefix.$table;

            $this->fields = $this->db->field_names($this->table);

        }

    }

}

Now error comes at $this->db->table_exists($table)

I know I can create db instance in this extended model. But I don't want to create that. I want to use existing one which is created in Controller.


Any help will be appreciated.Smile


Messages In This Thread
DB obj passed from controller to model - by El Forum - 02-09-2010, 09:57 AM
DB obj passed from controller to model - by El Forum - 02-09-2010, 11:12 AM
DB obj passed from controller to model - by El Forum - 02-09-2010, 11:13 AM
DB obj passed from controller to model - by El Forum - 02-09-2010, 11:24 AM



Theme © iAndrew 2016 - Forum software by © MyBB