<p>Have you tried it? I'm pretty sure that's not going to work as Active Record is tied to the db connection in <code>craft/config/db.php</code> hanging off of <code>craft()-&gt;db</code>.</p>

<p>You should be able to pull it off with a plugin that instantiates its own DbConnection, though and uses <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder" rel="nofollow noreferrer">Query Builder</a> instead of Active Record.  Completely untested, but something like this:</p>

<pre><code>$otherDbConnection =  Craft::createComponent(array(
    'emulatePrepare'    =&gt; true,
    'charset'           =&gt; 'utf8',
    'tablePrefix'       =&gt; 'craft_',
    'class'             =&gt; 'Craft\DbConnection',
    'autoConnect'       =&gt; true,
));

$otherDbConnection-&gt;connectionString = 'mysql:host=localhost;dbname=otherdb;port=3306';
$otherDbConnection-&gt;username = $userName;
$otherDbConnection-&gt;password = $password;
</code></pre>

<p>From there you can do <code>$otherDbConnection-&gt;createCommand()</code> and grab all of the data you want from the other database.  It's up to you how you'd merge that in with data from your existing database.</p>

<p>If you wanted to get fancy and access the database in other files/places in code, you could do:</p>

<pre><code>craft()-&gt;setComponent('otherDb', $otherDbConnection);
</code></pre>

<p>And access the other database via: <code>craft()-&gt;otherDb-&gt;createCommand()</code>.</p>

CC BY-SA 3.0
2015-04-02T00:14:20.730Z
Hey! Thanks for this. I was in a meeting so it took a while to acknowledge. It turns out I'd like to do it the fancy way, so I've put it a `config/db.php` in my plugin folder. Is it possible to tell Craft to bootstrap my plugin by loading this file first, or do I have to hack it by hardcoding it in `craft/app/bootstrap.php`?
charmeleon
CC BY-SA 3.0
2015-04-02T00:20:07.540Z
I wouldn't hack Craft's bootstrap.  Just do it from your plugin's init() method?
Brad Bell
CC BY-SA 3.0
2015-07-02T10:21:29.647Z
I'm trying to use code like that above, but get `Fatal error: Class 'DbConnection' not found in /var/www/mydomain.org.uk/craft/app/framework/YiiBase.php on line 213`. I'm *guessing* that `'DbConnection'` should be `'CDbConnection'`?
Phil Gyford
CC BY-SA 3.0
2015-07-02T16:45:09.097Z
Or `Craft\DbConnection`.
Brad Bell
CC BY-SA 3.0
2016-11-09T17:17:59.290Z
swapping out the class value: DbConnection for CDbConnection fixed that error.
Brad
CC BY-SA 3.0
2017-03-23T17:16:39.550Z
Actually you should change the class to 'Craft\DbConnection' and it will work as well, and then you have the craft db wrappers instead of the base yii interface.
Rick Kukiela
CC BY-SA 4.0
2018-12-15T01:58:39.010Z
You can see how the [Connect](https://github.com/nystudio107/craft-connect) plugin does it, if that helps ya.
andrew.welch
CC BY-SA 3.0
2015-04-01T22:24:38.113Z
2017-03-23T20:49:54.960Z
2017-03-23T20:49:54.960Z
Brad Bell
<p>I'm writing another plugin for my craft app, this time I'm attempting to reuse a MySQL table that's used by other applications as well. I've a class extending the BaseRecord, and <code>getTableName()</code> will return <code>differentdb.tablename</code>. Does Craft handle this, or do I have to make a copy of that table in the craft database? (MySQL permissions for the craft user already give it read access to that database)</p>

CC BY-SA 3.0
2015-04-01T21:28:25.230Z
Great question, I've wondered this myself.
Lindsey D
<database><config><models><records>
Query table in a different database
# Query table in a different database

(Asked by: username_0 on 2015-04-01)

I'm writing another plugin for my craft app, this time I'm attempting to reuse a MySQL table that's used by other applications as well. I've a class extending the BaseRecord, and `getTableName()` will return `differentdb.tablename`. Does Craft handle this, or do I have to make a copy of that table in the craft database? (MySQL permissions for the craft user already give it read access to that database)

**Tags:** database, config, models, records

> **Comments:**
> **username_1**: Great question, I've wondered this myself.  

---

## 1 answers

### Answer by username_2 on 2015-04-01. Score: 8 | Accepted answer

Have you tried it? I'm pretty sure that's not going to work as Active Record is tied to the db connection in craft/config/db.php hanging off of craft()->db.
You should be able to pull it off with a plugin that instantiates its own DbConnection, though and uses Query Builder instead of Active Record.  Completely untested, but something like this:
$otherDbConnection =  Craft::createComponent(array(
    'emulatePrepare'    => true,
    'charset'           => 'utf8',
    'tablePrefix'       => 'craft_',
    'class'             => 'Craft\DbConnection',
    'autoConnect'       => true,
));

$otherDbConnection->connectionString = 'mysql:host=localhost;dbname=otherdb;port=3306';
$otherDbConnection->username = $userName;
$otherDbConnection->password = $password;

From there you can do $otherDbConnection->createCommand() and grab all of the data you want from the other database.  It's up to you how you'd merge that in with data from your existing database.
If you wanted to get fancy and access the database in other files/places in code, you could do:
craft()->setComponent('otherDb', $otherDbConnection);

And access the other database via: craft()->otherDb->createCommand().

> **Comments:**
> **username_0**: Hey! Thanks for this. I was in a meeting so it took a while to acknowledge. It turns out I'd like to do it the fancy way, so I've put it a `config/db.php` in my plugin folder. Is it possible to tell Craft to bootstrap my plugin by loading this file first, or do I have to hack it by hardcoding it in `craft/app/bootstrap.php`?  
> **username_2**: I wouldn't hack Craft's bootstrap.  Just do it from your plugin's init() method?  
> **username_3**: I'm trying to use code like that above, but get `Fatal error: Class 'DbConnection' not found in /var/www/mydomain.org.uk/craft/app/framework/YiiBase.php on line 213`. I'm *guessing* that `'DbConnection'` should be `'CDbConnection'`?  
> **username_2**: Or `Craft\DbConnection`.  
> **username_4**: swapping out the class value: DbConnection for CDbConnection fixed that error.  
> **username_5**: Actually you should change the class to 'Craft\DbConnection' and it will work as well, and then you have the craft db wrappers instead of the base yii interface.  
> **username_6**: You can see how the [Connect](https://github.com/nystudio107/craft-connect) plugin does it, if that helps ya.  

---

