Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Roni's avatar
Level 33

A tethering relationship

Problem, 3 models

A hasMany B, B belongsTo A

A hasMany C, C belongsTo A

so B and C have an a_id in the tables. A has no reference to either of them, logically it's a binding object and has other data.

Is there a relationship I can build between B and C.

I can't use hasManyThrough since A has no reference to the other tables and indeed it can't since it's has many

Of course I can simply make a B()on C that returns a builder or collection and vice versa, but it will look different than all other relationships that work both as a field when resolved and a builder when called as a function.

Any advice on solutions or references would be greatly appreciated.

0 likes
1 reply
Roni's avatar
Level 33

So, this will work, I think it may have some performance losses on a large data solution, I'll have to run a performance test on it.

But the cheating solution is as follows

in B for example but works both ways

Since A has a function called cs() (plural of C):

I simply used it.

class B {
    
    public function cs(){
        return $this->a->cs();
    }
    
} 

The problem I can see is that $this->a adds an extra DB query so in some cases it would lead to an N+1 problem if used incorrectly.

Please or to participate in this conversation.