255], [['access_code'], 'string', 'max' => 4], [['sharer_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::class, 'targetAttribute' => ['sharer_id' => 'id']], [['access_code'], 'required', 'on' => self::SCENARIO_UPDATE], // 在 'update' 场景中,只验证 'access_code' 字段 ]; } /** * {@inheritdoc} */ public function attributeLabels(): array { return [ 'share_id' => '分享ID', 'sharer_id' => '分享者ID', 'file_relative_path' => '文件位置', 'access_code' => '访问密码', 'creation_date' => '分享创建日期', 'status' => 'Status', 'dl_count' => '下载次数', ]; } /** * Gets query for [[Sharer]]. * * @return ActiveQuery */ public function getSharer(): ActiveQuery { return $this->hasOne(User::class, ['id' => 'sharer_id']); } /** * Gets query for [[DownloadLogs]]. * * @return ActiveQuery */ public function getDownloadLogs(): ActiveQuery { return $this->hasMany(DownloadLogs::class, ['share_id' => 'share_id']); } /** * @return string|null */ public function getSharerUsername(): ?string { return $this->sharer->username; } /** * @return void */ public function setDlCountPlus1(): void { $this->dl_count += 1; $this->save(true, ['dl_count']); } /** * Give me the file name of the shared file/folder. * @return string */ public function getShareFileName(): string { return basename($this->getAbsoluteFilePath()); } public function getAbsoluteFilePath(): string { return Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $this->sharer_id . '/' . $this->file_relative_path; } }