A
A
Albert Tobacco2015-08-03 13:01:08
Yii
Albert Tobacco, 2015-08-03 13:01:08

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

2 answer(s)
M
matperez, 2015-08-03
@matperez

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.

L
LAV45, 2015-08-04
@LAV45

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 question

Ask a Question

731 491 924 answers to any question