true, 'extensions' => static::ACCEPT_FILE_EXTENSIONS ], [ 'user_id', 'default', 'value' => null ], [ 'user_id', 'integer' ], [ ['file_id'], 'exist', 'skipOnError' => true, 'targetClass' => File::class, 'targetAttribute' => ['file_id' => 'id'] ], [ ['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['user_id' => 'id'] ], ]; } public function behaviors() { return [TimestampBehavior::class]; } public function attributeLabels() { return [ 'file' => Yii::t('backend', 'Файл'), ]; } public function uploadFromPost(string $mainPageSettingId): void { $this->file = UploadedFile::getInstance($this, "[{$mainPageSettingId}]file"); if (!$this->upload()) { throw new RecordNotValid($this); } } public function buildSourceUrl(): string { return Url::to([ '/site/download-instruction-attachment', 'id' => $this->main_page_setting_id ]); } protected function getOwner() { return $this->hasOne(User::class, ['id' => 'user_id']); } protected function getOwnerId(): ?int { $owner = $this->owner; return $owner ? $owner->id : null; } public function getLinkedFile(): ActiveQuery { return $this->hasOne(File::class, ['id' => 'file_id']); } public function getExtensions(): string { $linkedFile = $this->linkedFile; if (!$linkedFile) { return ''; } $splitUploadName = explode('.', $linkedFile->upload_name); return end($splitUploadName); } public static function getInstructionData( array $postData, string $instructionForm, string $mainPageSettingId ): array { return array_merge( parent::getInstructionData($postData, $instructionForm, $mainPageSettingId), ArrayHelper::getValue($_FILES, "{$instructionForm}.name.{$mainPageSettingId}", []) ); } }