Answer the question
In order to leave comments, you need to log in
Private properties of ActiveRecord Yii2?
I would like to declare some properties of the model as private. Let's say the `status` cell in the `order` table I only want to set via the `setStatus()` method. Personally, nothing prevents me from starting such a method and using it only.
But I'd like to exclude other options for setting `order` other than through the `setStatus` method. Can I do it?
Answer the question
In order to leave comments, you need to log in
For example, you can intercept field access in __get/__set magic methods and throw an exception. There is no such functionality out of the box.
setters and getters are primarily used to modify data as it is received or assigned.
Read more about ActiveRecord::scenarios()
https://github.com/yiisoft/yii2/blob/master/docs/g...
class Order extends ActiveRecord
{
public function scenarios()
{
$scenarios = parent::scenarios();
$scenarios[self::SCENARIO_DEFAULT] = [/** Все поля без поля status */];
$scenarios['editWithStatus'] = [/** Все поля */];
return $scenarios;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question