A
A
alestro2016-05-17 19:48:36
PHP
alestro, 2016-05-17 19:48:36

Will this functionality be redundant for the class?

There is a Game class that is a video game, as well as a GameMapper that pulls information from the database and returns an object of the Game class. There are also similar Post and PostMapper classes, respectively. So, since a game can have news that belongs only to this game, you should set the posts property and store a collection of post objects there. Having previously implemented PostMapper in the GameMapper's property, wouldn't that be redundant? Currently implemented as follows:

$game=$container->make('GameMapper')->find($uri);// Вытаскиваю из базы игру по запрошенному uri
  View::render
  (	'single',
    [
      'game'=>$game, 
      'posts'=>$container->make('PostMapper')->find_posts_for_game($game->id())// Вытаскиваю новости по id игры
    ]
  );

After performing the above manipulations, I get the following:
View::render
  (	'single',
    [
      'game'=>$game=$container->make('GameMapper')->find($uri);// Вытаскиваю из базы игру по запрошенному uri
    ]
  );

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey, 2016-05-17
Protko @Fesor

wouldn't that be redundant?

You will be injecting services into your entities, and this is already showing a bad decision.
In tools like Doctrine, this is solved by wrapping the entity object in a proxy class that, when accessing the field, will load the required entities. In this case, our entity is a simple PHP object. and the proxy object is responsible for the magic. Maximum flexibility. Well, it turns out that when we create an entity - we have it just an object, when we get it from the base - an object already wrapped in a proxy.
In a word ... yes, this is a violation of the single responsibility principle and the entity begins to do things that are inappropriate for it. But on the other hand, the simplicity of implementation makes this approach more than convenient. So if the question is exactly "how to do" and at the same time you do not want to use ready-made tools like Doctrine, writing something like this yourself will be wildly expensive and therefore it is better to make a compromise.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question