'afterSave', ActiveRecord::EVENT_AFTER_UPDATE => 'afterSave', ActiveRecord::EVENT_INIT => 'attachValidator', ]; } public function attachValidator($event) { $validator = Validator::createValidator('safe', $this->owner, 'fileloader'); $this->owner->validators->append($validator); } /** * After saving model delete all relative files and insert new file connections from * fileloader variable * * @param Event $event */ public function afterSave($event) { /** * @var ActiveRecord $owner * @var string $relation */ $owner = $this->owner; $relation = $this->relationclass; call_user_func([ $relation, 'deleteAll', ], [ 'model' => $owner->className(), 'model_id' => $owner->primaryKey, ]); if(!empty( $owner->fileloader )) { foreach($owner->fileloader as $file) { /** * @var ActiveRecord $model */ $model = new $relation([ 'file_id' => $file, 'model' => $owner->className(), 'model_id' => $owner->primaryKey, 'user_id' => \Yii::$app->user->getId(), 'status' => 1, ]); if($model->validate()) { $model->save(false); } unset( $model ); } } } /** * Bind owner class tp specified $fileclass via $relationclass table. * @return ActiveQuery */ public function getFileloaderFiles() { /** * @var ActiveRecord $owner */ $owner = $this->owner; $relationtable = call_user_func([ $this->relationclass, 'tableName', ]); return $owner->hasMany($this->fileclass, [ 'file_id' => 'file_id' ]) ->viaTable($relationtable, [ 'model_id' => $owner->primaryKey()[ 0 ] ], function($query) use ($owner) { /** * @var ActiveQuery $query */ $query->andWhere([ 'model' => $owner->className(), 'status' => 1, ]); }); } }