A
A
Alexey Nikolaev2018-10-23 16:05:47
PHP
Alexey Nikolaev, 2018-10-23 16:05:47

Is it normal for an ActiveRecord model to be a model factory?

Good day.
There is a general Stats model, and there are submodels that describe a specific entity (ServiceOneStats, ServiceTwoStats). I consider it incorrect to share, tk. entity type is the same - statistics. Each of the models has its own table. I would like to get an instance of the Stats model with a certain type, so that I can immediately access the data of the submodel.
Some pseudocode below.

$StatsModel = new Models\Stats(1, 'service-one'); 
$StatsModel->serviceOneField;

$StatsModel = new Models\Stats(2, 'service-two'); 
$StatsModel->serviceTwoField;

The difficulty is that Stats also has a table, with id, title, and a couple of other common fields (creation date, etc.).
The question is: is it normal for an AR model to act as a factory/refer to a factory? After all, I will have to somehow get child models in order to extract data from them in the correct (in the framework ecosystem) method. Perhaps there is some pattern that solves the problem of obtaining data from inherited models (subentities)? Or is it the opposite, an antipattern, and models cannot be inherited?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question