T
T
tester_toster2018-04-12 15:37:24
PHP
tester_toster, 2018-04-12 15:37:24

How to solve the problem of building several types of entities with the possibility of incomplete filling of an entity from one database table?

There is a table in the database.
Each table row can be a specific entity, for example: "news, article, novelty", etc.
They have both the same parameters and different ones, for example: a title and a link, everyone has it, only a novelty has a rating.
To implement the getters of these parameters, I created an abstract class, and defined a constructor in it:

abstract class Base
{
    protected $data;

    private $categoryIds;

    public function __construct(array $params)
    {
        $this->data = new NullObject($params);
    }

   public function getTitle() :string
   {
      return $this->data->title;
   }
   
   //...
}

There is a factory that, according to certain data criteria, creates an object of a new class (News, Article), etc.
The constructor receives an array of data, which is placed in a null object, and the methods are already pulling data from it. It is inconvenient to paint a constructor, sometimes an array containing more than 10 elements comes in.
Also, the methods of some entities intersect, and in order not to write several times in a row, I simply inherit the entity not from Post, but from the entity that has implementations of these methods. But at the same time, methods that I do not need are also inherited.
What is better to do: add mixins, or nullify the implementations in the child class, or something else?
Plus, sometimes you need to select a collection of objects - to display a list, all the data is not needed there, and therefore I just feed an array there with less data. In principle, everything suits me. But it turns out that if someone wants to call a method using data that did not come into the object's constructor, they will receive an error. It is not desirable to create another object of the ShortNews type, because in different places you need to select different data - many objects will spawn + extra logic when building.
I would be grateful for any thoughts.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander, 2018-04-12
@Minifets

What you are doing, there is such an implementation in Doctrine . You can see how it is implemented there.

M
Maxim Fedorov, 2018-04-12
@qonand

Without describing the parameters themselves, it is difficult to understand the difference between the entities, in general, the correctness of your decision and give more or less adequate comments. The only thing I can assume, based on your description, is that you did not correctly build the inheritance hierarchy. If it were correct, such questions would not arise. Therefore, you should reconsider and rethink it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question