Welcome Guest, Not a member yet? Register   Sign In
Is this a correct way to connect to the database?
#1

[eluser]carlosgoce[/eluser]
Hello,

i have like 4-5 database connections between odbc, sqlite, mysql, postgree... etc. I have a little headache with all of this. Finally my solution was to create the database connection on the constructor of the model.

Something like this:
Code:
Class My_customer extends CI_Model
{
    public $id;
    public $customer;
    private $dbMysql;

    function __construct()
    {
        parent::__construct();
        $this->dbMysql = $this->load->database('My_database', TRUE);
    }
}

But i have a doubt, if i do something like this:
Code:
$customersList = array();
foreach ($data AS $customer)
{
     $customersList[] = new My_customer();  
}

Did i get tons of connections or is the same for all?

I need to use my clases as POJO but also as a model to insert the data. But i only want 1 single connection.

Another way could be this:
Code:
$db1  = $this->load->database('db1');
        $db2  = $this->load->database('db2');
        $this->load->model('Modelo1', '', $db1);
        var_dump($this->db); //show db1 connection
        $this->load->model('Modelo2'', $db2);
        var_dump($this->db); //show db2 connection

So inside model1, after load model2, it'll be working with db2 connection instead of db1. I don't know how to fix this.


Thank you for your time.

By the way, it is correct to put public all the params of the class that are fields of the table? So i can do a insert($customer), if the params are not public i can't do that.




Theme © iAndrew 2016 - Forum software by © MyBB