rsanchez / Deep

A set of Eloquent models for ExpressionEngine Channel Entries.
http://rsanchez.github.io/Deep/
MIT License
51 stars 14 forks source link

Is it possible to set a separate DB connection? #19

Closed tobz-nz closed 9 years ago

tobz-nz commented 9 years ago

I thought this should work: Entries::setConnection('ee'); but no go. I could probably extend the Entries model and add a $connection parameter, but that seems a bit hacky.

Any ideas?

rsanchez commented 9 years ago

This is how the Eloquent docs say to handle this (in the Specifying The Query Connection section):

Entries::on('ee')->find(1);
tobz-nz commented 9 years ago

Yeah, I tried that, it doesn't seem to work.

tobz-nz commented 9 years ago

In the meantime, I've swapped my default connection around, so default is 'ee' and then in my none EE models I'm setting the $connection to 'mysql'.

This approach is working, not ideal though. It'd be nice to have a config item or something for the connection to use for all Deep queries.

tobz-nz commented 9 years ago

oh wait... on() is working now. hmm not sure what happened there. Never mind then.

tobz-nz commented 9 years ago

.. may have spoken too soon.

I think it works the the first level, but any subsequent queries - eg getting file field data - do not inherit the connection name.

rsanchez commented 9 years ago

You're right, on won't work. I'll have to come up with some way to set the connection global for all of Deep's models.

rsanchez commented 9 years ago

I just bumped to version 1.1.2, which contains the ability to set the connection globally via config. Added a note to the Readme at the bottom of this section: https://github.com/rsanchez/Deep#laravel

So in your case you'd add this to your app/config/database.php:

'deep' => array(
    'connection' => 'ee',
),
tobz-nz commented 9 years ago

oh sweet. thanks mate. Will give it a go tomorrow.

tobz-nz commented 9 years ago

hmm, the new config doesn't seem to be working. getting sql errors:

Table 'fusion.channels' doesn't exist (SQL: select * from `channels`)

My main problem is that all my EE tables are prefixed with exp_ as per default. but my none EE tables are not. (this is a kind of bastard hybrid app that is slowly migrating away from EE)

rsanchez commented 9 years ago

Take a look here: https://github.com/rsanchez/Deep/blob/master/src/App/Laravel/ServiceProvider.php#L17-L20

That's where the global connection for Deep gets set. Maybe check if the config item database.deep.connection is actually getting picked up there? That config should go in your database.php.

tobz-nz commented 9 years ago

aha, found me problem - hadn't added teh service provider to app.php. #derp All working now. Thanks for adding that feature in so quickly, really appreciated :)

tobz-nz commented 9 years ago

... one thing - I'm having to force the version to 1.1.2 in composer.json to get it. ~1.1 (or 1.1.*) doesn't pull in 1.1.2 which it should.

rsanchez commented 9 years ago

In an empty dir i tested composer require "rsanchez/deep" "~1.1" and it installed v 1.1.2. So I'll chalk that up to composer caching weirdness or a momentary packagist snafu.

tobz-nz commented 9 years ago

sweet as. .. just remembered I'm using a Toran Proxy instance on this project so it's probably to do with that.