Auto-loading Models in mvc
If you want to perform particular model
globally in your application, you can tell CodeIgniter in mvc pattern to auto-load model during system execution.
This is done by file the application/config/autoload.php file and adding the
model to the autoload array.
Once a model is loaded it does NOT means to connect automatically database. The below
options for connecting are available in CodeIgniter
and mvc:
·
You can connect database from a model using the standard
database methods , either from
within your Controller class or your Model class.
·
You can tell the model loading function to auto-connect by
passing TRUE (boolean) via the third parameter, and
connectivity settings, as defined in your database config file will be used:
$this->load->model('Model_name', '', TRUE);
You can manually pass database connectivity
settings via the third parameter:
$config['hostname'] = "localhost";
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$this->load->model('Model_name', '', $config);
$config['username'] = "myusername";
$config['password'] = "mypassword";
$config['database'] = "mydatabase";
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$this->load->model('Model_name', '', $config);
No comments:
Post a Comment