compoships icon indicating copy to clipboard operation
compoships copied to clipboard

Including support for composite primary keys

Open pedrofmj opened this issue 3 years ago • 4 comments

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?

pedrofmj avatar Oct 23 '20 16:10 pedrofmj

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.

topclaudy avatar Oct 23 '20 21:10 topclaudy

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.

pedrofmj avatar Oct 29 '20 09:10 pedrofmj

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

sinnbeck avatar Jan 19 '21 07:01 sinnbeck

@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

pedrofmj avatar Jan 19 '21 10:01 pedrofmj