#Is it possible to define different connection for a hasOne Relationship?

1 messages · Page 1 of 1 (latest)

exotic grove
#

Hi there, this might be a stupid question but is it possible to define a different connection for a hasOne relationship when using the with() condition?
Example: Model::on($firstConnection)->with(['route_group' => fn ($q) => $q->connection($SecondConnection)])...

worldly berry
exotic grove
#

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?

worldly berry
#

What is the problem you’re trying to solve here?

exotic grove
#

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

worldly berry
#

Why on earth do you have data split across four databases?

exotic grove
#

The initial query: ‘model::on()’ works

exotic grove
worldly berry
#

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.

exotic grove
#

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

worldly berry
exotic grove
#

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

worldly berry
#

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.

exotic grove
#

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?

worldly berry
#

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.

exotic grove
#

Well, here’s to hoping he comes back in 2 months to fix it