Hello! Learn php, a smooth transition from velosipedisty to best practices. Engineering once the application realized that it would be very appropriate to use the Repository pattern to separate business logic from CRUD + use different repositories for different entities.
Did two interfaces IOUserRepository, IOPostRepository for entities User and Post sootvestvenno.
Classes for working with databases that implement these interfaces MySQLUserRepository and RedisPostRepository themselves the entity classes User and Postthat contain logic (e.g. User->ban(), Post->approve() , etc.)
And once ripe the question, and how to organize the relationship of these entities? Ie if you are using ActiveRecord , I did that User::find(1)->post()->where('is_approved', '0')->first()->approve() (example for ELoquent). One of the outputs I see:
1) In the class User do array $posts
2) In the class RedisPostRepository do method getPostsByUserID($user_id)that returns an array of articles for the user
3) In the load()method of class MySQLUserRepository caused by the above mentioned method to populate the array $posts from paragraph 1, but you need to pass an object of a class that implements IOPostRepository ( in the DI container makes binding interface->class, is used Pimple)
The other, in the database relationship one to many (one User -> many Posts).
It turns out, when you use this pattern to do the connection at the level of repositories ?
PS it is Very possible that I misunderstood the essence of this pattern. If so, please poke his nose in mistakes.
themselves the entity classes User and Post that contain logic (e.g. User->ban(), Post->approve (), etc.) This is not correct, it should be in the service level
It turns out, when you use this pattern to do the connection at the level of repositories ? right
A transaction to run in services