#Is it possible to define different connection for a hasOne Relationship?
1 messages · Page 1 of 1 (latest)
No. You can’t create relationships between tables in different databases.
Damn, okay second question. Can I define a dynamic connection to a model? I have a function that tests 4 connections and returns the first successful connection name
Do i need to do some like public function __construct in the model to assign that?
What is the problem you’re trying to solve here?
So I’m trying to fetch data from a table that exists in 4 databases. I want to do a hasOne relationship on that model which also exists in 4 databases
Why on earth do you have data split across four databases?
The initial query: ‘model::on()’ works
If I knew the answer, I would tell you but somehow our database engineer thought it would be a good idea
You can’t just keep querying until you find a result; that’s wholly inefficient because now you’re having to do up to 4x as many queries as you need to.
Oh no, I’m not doing the queries on each connection. I’m doing a separate function that checks what connection is active and returns the connection to the controller (like a helper function)
The problem is that I need to be able to provide that connection name to the hasOne (‘with()’) relationship too but don’t know how
I’ve turned off my PC for now otherwise I would’ve sent you code snippets
So I’m a nutshell, we have 4 servers across Australia that are in a cluster so to speak. One dies, another can be used to continue work without impacting data loss.
In*
On certain models, I have to run another function before hand to see which of the 4 servers/connections is active and then return the active connection back to the controller. That’s what is used in the model using the on() property or method
That’s just replication. Your application shouldn’t know about them. It should just make a connection and something else determines what database it actually connects to.
Yeah, there’s a thing called Galera that does that
For some reason, our database engineer also made 4 databases on their own and is syncing them somehow…
I knew it was gonna be a shitshow so I didn’t bother with it but now I have to deal with it somehow FFS
The GALERA cluster is one connection that handles multiple nodes. One dies, another is used but laravel doesn’t know that a node is dead in the middle
I do use that for certain models and it’s amazing but this manual replication is killing me and eloquent
But is that not the point of using something in front of the nodes? It should be picking an available node when you connect to it. Otherwise it’s useless.
Seems our engineer didn’t want to or didn’t know how to so I’m trapped with this
Any laravel based solution or do I say screw it and use one connection hoping it doesn’t fail?
This isn’t a problem that needs fixing via code. This is a, your engineer needs to set up an actual cluster with failover and gives you a single load balancer to connect to, instead of just guessing which one of four connections you should be using on every page load.
Well, here’s to hoping he comes back in 2 months to fix it