compoships icon indicating copy to clipboard operation
compoships copied to clipboard

Relate two different foreign keys to single local key

Open KonradHambuch opened this issue 10 months ago • 0 comments

I have a table exchanges that has standard_parcel_id and return_parcel_id. I want my Parcel model have an Exchange, so my relationship would be:

public function exchange(){
        return $this->hasOne(Exchange::class, ['standard_parcel_id', 'return_parcel_id'], ['id', 'id']);
    }

In this case no parcel finds its exchanges table record. If I use only one 'id', it will work for the first foreign key, but not for the second one. So in this case:

public function exchange(){
        return $this->hasOne(Exchange::class, ['standard_parcel_id', 'return_parcel_id'], ['id']);
    }

standard parcels find their Exchange, but return parcels do not.

Is there a workaround here?

KonradHambuch avatar Apr 09 '24 10:04 KonradHambuch