As rightly action menu the default value in Yii2?

The situation when it is necessary to attribute in the model the default value is very frequent, as it can be implemented in Yii2 there is mnustwo ways, but how will the correct?? Yii2 not so long ago, learn from here and ask this question. So that shrank to find, for example, user_id of the current session:

1) Prescribe the method beforeValidation
public function beforeValidation(){
$this->user_id = Yii::$app->user->identity->id

1.5) or beforeSave

public function beforeSave($insert){
$this->user_id = Yii::$app->user->identity->id

2) In the model rules prescribe:
public function rules()
 return [
 ['user_id', 'integer'],
 ['user_id', 'default', 'value' => Yii::$app->user->identity->id ],

3) use behaviors:

public function behaviors()
 return [
 'blame' => [
 'class' => BlameableBehavior::className(),
 'attributes' => [
 BaseActiveRecord::EVENT_BEFORE_INSERT => 'user_id'

4) There are a variant with transfer into the model when declaring the object
$model = new SomeThing(['user_id' => Yii::$app->user->identity->id ]);

So how is more correct in a situation specify the current user by default?
July 9th 19 at 13:37
2 answers
July 9th 19 at 13:39
better to use option number 2 because it more clearly sets the default value, furthermore after applying a default-validator is able to check specified value using other validators.
The c afterSave and beforeSave - make code less readable.
Variant behaviors - will give a lot of extra code and complicate its understanding
The option of passing it to the model when you declare a value object that is DEFAULT, actually not true, because in every place where an object is created it will have to register
July 9th 19 at 13:41
beforeValidation - will not fire, for example, when $model->save(false)
beforeSave - will always work whether You like it or not
rules - a good option for most tasks, but you must understand that there will be null if the user is not logged in
behaviors - a case not just values, and some logic, it is convenient if not necessary and a possible null in the previous version
4 option - a convenient if in each case different user_id + 'll get along with rules or behaviors
Even when there are negative rules in the form: ['user_id', 'required'] in this case, the default will not work and will error. On the other hand, most likely in this case, Monge neglected 'required' and to refuse it.. - lou commented on July 9th 19 at 13:44
: Well, it's either the default or a mandatory attribute. - Henderson_Beat commented on July 9th 19 at 13:47

Find more questions by tags PHPYii