Answer the question
In order to leave comments, you need to log in
How to create objects of different classes depending on an attribute in Yii2?
There is a basic model, there are its heirs. It is necessary that, depending on the attribute, for example type, a new model of the {Type}{BaseModel} class is created, if type is specified, or of the {BaseModel} class, if type is not specified. How can this be implemented?
Answer the question
In order to leave comments, you need to log in
To do this, you need to override the ActiveRecord instantiate static method, which is responsible for creating the model object
static function instantiate($row)
{
if ($row['type'] == self::TYPE_ARTICE)
return new Article();
return new static();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question