tpetry / laravel-postgresql-enhanced

Support for many missing PostgreSQL specific features
MIT License
772 stars 31 forks source link

ltree relationships #81

Open boris-glumpler opened 6 months ago

boris-glumpler commented 6 months ago

Just wondering out loud here, but would it be within the scope of this package to add relationships based on an ltree column? So something like children, parent and ancestors. Maybe even to a given depth?

I've just used ltree for a comments system and ended up doing two queries. The first one gets a paginator just for the first level and the second one gets any children for the items in that paginator. I then have a tree class that builds up the hierarchy, sorts it properly and sets the resulting collection back on the paginator. It'll work just fine for my use case as there won't be many comments at all and even less nested ones, but it got me wondering if this couldn't be solved more efficiently.

tpetry commented 6 months ago

Hmm, I‘ve not implemented a custom relationship class yet. But it could work. I think I could have a look at non-ltree tree relationship implementations.

I guess then also a model cast would make sense. But in what should it be casted? An object that splits the parts in array elements?

boris-glumpler commented 6 months ago

I haven't either. I googled for a bit and found this repo, but it does way too much for my liking, tbh: https://github.com/umbrellio/laravel-ltree

I'm not sure what you mean with the model cast? Like a tree collection? The way I handled it was add a regular children property to my comment model (that was all I needed) and then just populated those within my tree builder. Doesn't seem very laravely, tbh ;)

tpetry commented 6 months ago

So you're only interested in the BelongsToAncestorsTree and BelongsToDescendantsTree relationships from that package? Nothing more? I think at least scopes are also needed.

Its a nice idea. But I probably don't have the time for it currently. I've never implemented a relationship. So it needs a lot of fiddling around to find out how it works.

boris-glumpler commented 6 months ago

Yup, scopes do make sense.

Don't worry about working on it for now 😉 What I have works for my current project just fine and should it turn out later to not be enough, then I might give it a go myself at a later date.