odan / slim4-skeleton

A Slim 4 Skeleton
https://odan.github.io/slim4-skeleton/
MIT License
439 stars 80 forks source link

Handling relationships within the skeleton structure #79

Closed curVV closed 2 years ago

curVV commented 2 years ago

Any pointers on how to start looking at handling database relationships within this structure of factories/repositories/data/services etc?

Anything I think of involves either rewriting just about everything and/or making too many queries instead of one join query or feels like I'm trying to reinvent an ORM. I might be missing the obvious since I'm still having a hard time wrapping my head around all the fractions of logic in different places.

odan commented 2 years ago

Hi @curVV

Yes, you don't need to reinvent an ORM.

The documentation of this project contains more details. This page explains more about Repositories.

In my blog and this Slim 4 eBook you can take a deeper look at these topics. Example: Slim 4 - Cake QueryBuilder

And last but not least: If you have a specific question, you can file an issue here or consult me directly.

curVV commented 2 years ago

Hi, sorry I wasn't being clear. I've read the documentation and I understand how to use joins using the query builder. But it is not clear to me how to implement these relationships into the structure set out in the skeleton.

Say for instance I have a table called articles that contain articles that belong to users. If I want to implement this relationship in the current structure of the skeleton, how would you go about doing this? The UserRepository->getUserById() method returns a UserData object. Should I add a UserData->articles() method that would call something like ArticlesRepository->getArticlesbyUserId($this->id) so I could do $user->articles()? But that just means a separate query, no joins performed. That does not seem efficient to me. Trying to think of other ways...

odan commented 2 years ago

In my eBook I explain the Clean Architecture much more in detail.

The key point is to "think" and build software in use cases. So it's a complete "mind shift", if you will.

Here is a collection of very similar questions where you can also find some answers. I hope it helps.

curVV commented 2 years ago

Makes sense, thank you.