Second MySQL DB Connection with Doctrine

Permalink 2 users found helpful
OK, it's been a while since I use Concrete5. And everything seems different now especially witn Doctrine for database connection and ORM.

Here's what I plan to do. I have another database that contains information that I need to display on my Concrete5 site. It has no Concrete5 tables. Just my own tables.

My plan is to create a class and store it in application/src/MyData/MyData.php. I need to know how to connect to my second database connection from here. I do have my second MySQL connection details in the array in application/config/database.php. I am not to sure about how to use the DatabaseManager and/or Doctrine to connect to it. I did find the other thread about connecting a second DB, but that is for MongoDB. I am lost... haven't used Doctrine before.

Continuing from that, if I get MyData.php to connect to my second MySQL, my next plan is create a block that will instantiate MyData class and get the data. In the block controller, I would probably have

The plan to be used in my block controller
$mydata = new MyData();
$display_data = $mydata->get($limit, $page);

Snippet of my database.php
return array(
    'default-connection' => 'concrete',
    'connections' => array(
        'concrete' => array(
            'driver' => 'c5_pdo_mysql',
            'server' => 'localhost',
            'database' => 'plainc5',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8'
        ),
        'second_db_conn' => array(
            'driver' => 'c5_pdo_mysql',
            'server' => 'localhost',
            'database' => 'my_data_db',

Honestly, I am lost as to how I can accomplish this -- the second mysql connection with doctrine.

 
kidino replied on at Permalink Reply
OK. I though it was going to be simple. Do correct me if this is the not right way to do it. Here's how I make my second connection in a general class. My class in application/src/MyData/MyData.php. And in the __constructor, I added
$this->conn = \Database::connection('connection_name');

"connection_name" comes from my database.php, where I set my second database settings. Now I can use it anywhere within the class by referring to $this->conn, for example
$stmt = $this->conn->query("select * from widget");

There you go!
Korvin replied on at Permalink Reply
Korvin
This is correct, you add your new connection configuration and load it with
\Database::connection('second_db_conn');
kidino replied on at Permalink Reply
Thanks for confirming this. I was scratching my head for days before I found this method.