Welcome Guest, Not a member yet? Register   Sign In
Autoload a model and autoconnect to a database
#1

[eluser]GamingFusion[/eluser]
i would like to autoload a model then have that model auto connect to a database. i have read the user guide on it tells me as far as i know how to connect to by loading the model from a controller.

So could i load it in the parent constructor of my controller? or how

Quote:When a model is loaded it does NOT connect automatically to your database. The following options for connecting are available to you:

* You can connect using the standard database methods described here, 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);
#2

[eluser]Thorpe Obazee[/eluser]
Just autoload the model in 'application/config/autoload.php'. You won't need to manually connect to the database using '$this->load->database('group_name');'
#3

[eluser]GamingFusion[/eluser]
ok thanks man
#4

[eluser]Thorpe Obazee[/eluser]
no problem Smile
#5

[eluser]Renier[/eluser]
[quote author="bargainph" date="1241488386"]Just autoload the model in 'application/config/autoload.php'. You won't need to manually connect to the database using '$this->load->database('group_name');'[/quote]

When I do as described above the model does load, but NOT connect to the database. When i load the model inline like beneath it works fine:

Code:
$this->load->model('MODELNAME', '', True);

Do i have to configure something else? I do load the database by default...

I am using codeIgniter 2.1.0.
#6

[eluser]InsiteFX[/eluser]
Because you also need to autoload the database in libraries ./application/config/autoload.php!
#7

[eluser]Renier[/eluser]
[quote author="InsiteFX" date="1334325055"]Because you also need to autolaod the database in libraries ./application/config/autoload.php!
[/quote]

thanks!




Theme © iAndrew 2016 - Forum software by © MyBB