A
A
Alex R2018-02-24 16:30:10
PHP
Alex R, 2018-02-24 16:30:10

How to use the Repository correctly?

Hello! I am learning php, smoothly moving from cycling to best practices. When designing another application, I realized that it would be very appropriate to use the Repository pattern to separate business logic from CRUD + use different storage for different entities.
Made two interfaces IOUserRepository , IOPostRepository for User and Post entities respectively.
Classes for working with the database that implement these MySQLUserRepository and RedisPostRepository interfaces and the User and Post entity classes themselves that contain logic (for example , User->ban() , Post->approve()etc.)
And the question immediately arose, how to organize the connection of these entities? That is, when using ActiveRecord , I did this User::find(1)->post()->where('is_approved', '0')->first()->approve() (example for ELoquent). One of the outputs that I see:
1) In the User class, we make an array of $posts
2) In the RedisPostRepository class, we make the getPostsByUserID($user_id) method , which returns an array of articles for the user
3) In the load() method of the MySQLUserRepository class , we call the above method, for filling the $posts array from point 1, but for this you need to pass an object of a class that implementsIOPostRepository (does interface->class binding in the DI container, Pimple is used) By
itself, in the database, the relationship is one-to-many (one User -> many Posts).
It turns out that when using this pattern, you need to make a connection at the level of repositories?
PS It is very possible that I still misunderstood the essence of this pattern. If so, please point out the errors.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
synapse_people, 2018-02-24
@LazyDaemon

the User and Post entity classes themselves, which contain logic (for example, User->ban(), Post->approve(), etc.)
layer repository level?
And start transactions in services

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question