/** * Делает обход дерева от модели вверх по предкам до корня. * Возвращает первое не пустое значение атрибута обходимых моделей. * @param TActiveRecord $model модель * @param string $attr атрибут * @param bool $default значение по умолчанию * @return bool|mixed */ public function findValue(TActiveRecord $model, $attr, $default = false) { if (!empty($model->{$attr})) { return $model->{$attr}; } $models = $model->parents()->all(); $models = array_reverse($models); foreach ($models as $m) { if (!empty($m->{$attr})) { return $m->{$attr}; } } return $default; }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->isNewRecord and $this->scenario == ActiveRecord::SCENARIO_INSERT and !Yii::$app->user->isGuest) { $this->username = Yii::$app->user->identity->username; $this->email = Yii::$app->user->identity->email; } }