Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Specifying a database connection per model - relating across DBs.
  • I'm trying to integrate Fuel with an old cluster of a project. There's at least 2 databases that have models that should be related. I know, I know. I just inherited this POS. The databases are already established and I can't change them... yet. So I was wondering if it's possible to setup multiple database connections, and specify what connection to use in the models. Probably not possible to relate models across DBs, but I'm wishful. Is it doable?
  • You can set the connection name: http://fuelphp.com/docs/packages/orm/creating_models.html#connection You can't connect between databases yet however, it's planned but not yet possible (using joins now, needs to support where-in for that). If all databases are accessible on the same server with same user/pass you can however use just 1 connection and set tablenames including their database: "database.table" - that might work across DBs.
  • You're right. As long as the DBs are in the same server, it is doable. There's 2 things going on that can make this work.
    1) If the join table is in the DB of the related model, then it is easily found ($user->group userGroup is found in DB of group.)
    2) If you prefix the 'through' table with the DB name, then it works. ($user->group 'table_through' => 'second_db.userGroup',)
  • Actually, it only works if you do a ->find on a model first, then get the relation. If you do a ->query()->related() it'll die with the 'Models cannot be related between connection' error.
  • Jelmer Schreuder wrote on Thursday 20th of October 2011:
    You can set the connection name: http://fuelphp.com/docs/packages/orm/creating_models.html#connection You can't connect between databases yet however, it's planned but not yet possible (using joins now, needs to support where-in for that). If all databases are accessible on the same server with same user/pass you can however use just 1 connection and set tablenames including their database: "database.table" - that might work across DBs.
    But, where i will set the database in my relation? Not using ORM package, just on DB class?

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion