Saeid Raei
1 min readAug 26, 2022

--

The thing you got wrong about the `$user->getPosts()` is that the entity itself doesn't have anything to do with the database, what happens here is that the doctrine is watching the entity when you fetch them from database and where the `$user->getPosts()`method is called the listener would fetch the needed data, this means that you can still reuse the Model since the information about the database itself is in attributes that doesn't affect the class itself, now in laravel there is no way you can make the relation methods make sense in a repository pattern (DTOs needed).

About reusing code, you provided the best example yourself, imagine if that piece of code is using the relations on the model, now in laravel you can't reuse the code but in symfony you can manually create the Model or provide it from somewhere else.

About your Post Interface solution, I thought it would solve the problem that we are discussing in repository pattern, I've already used DI in the original post.

in many cases repository pattern will not be needed, nor the database is gonna change, or scaling out to multiple servers will be needed, in those cases I agree that it's a better idea to not use any of these ideas at all and stick with just laravel.

--

--

Saeid Raei
Saeid Raei

Responses (1)