topclaudy / compoships

Multi-columns relationships for Laravel's Eloquent ORM
MIT License
1.12k stars 130 forks source link

Including support for composite primary keys #102

Open pedrofmj opened 4 years ago

pedrofmj commented 4 years ago

Hello,

I currently use compoships in conjunction with a Trait made by myself that enables Composite Primary Keys. Would you guys think it would be useful and meaningful including such trait into Compoships? Since we are talking about composite foreign keys in compoships, it onlly makes sense if we also we have support for composite primary keys. If it makes sense, I could do some code for it. What do you think?

topclaudy commented 4 years ago

As stated in the documentation:

There are situations where you'll need to match many columns in the definition of a relationship even when your models' tables have a single primary key.

So, Compoships allows us to define relationships with multiple keys, but those keys can be any set of columns, not just primary keys.

pedrofmj commented 4 years ago

Hi @topclaudy , I fully understand your point. But what I mean is that if we are adding support to composite foreign keys with compoships, it also makes sense to add support for composite primary keys in the same package, so that one that wants to use compoships also can set composite primary keys, that is not natively supported by Eloquent/Laravel. I have a class for this, and I could add it to compoships.

sinnbeck commented 3 years ago

@pedrofmj Could you perhaps post the class? I would like to give it a try

pedrofmj commented 3 years ago

@sinnbeck , it's a trait, called HasCompositePrimaryKey. You need to use that trait in your model. It's also important to correctly set the primaryKey in the model, (ex.: protected $primaryKey = ['key1', 'key2', ...]).

Here's the gist for the code:

https://gist.github.com/pedrofmj/614061eff182626b7f2056073edc600e