diff --git a/controllers/ShareController.php b/controllers/ShareController.php index c25a9b8..650fccc 100644 --- a/controllers/ShareController.php +++ b/controllers/ShareController.php @@ -271,6 +271,8 @@ class ShareController extends Controller $model = $this->findModel($share_id, true); DownloadLogs::addLog(Yii::$app->user->id, $model->share_id, Yii::$app->request->userIP, Yii::$app->request->userAgent); // logging for access(DL) + // add download count + $model->setDlCountPlus1(); $absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . $model->sharer_id . '/' . $model->file_relative_path; if (is_file($absolutePath)) { return Yii::$app->response->sendFile($absolutePath); diff --git a/models/Share.php b/models/Share.php index d00615d..be08b69 100644 --- a/models/Share.php +++ b/models/Share.php @@ -14,12 +14,15 @@ use yii\db\ActiveRecord; * @property string $access_code 分享密钥 * @property string $creation_date 分享创建日期 * @property int|null $status 分享是否启用 + * @property int|null $dl_count 下载次数 * - * @property User $sharer + * @property DownloadLogs[] $downloadLogs + * * @property User $sharer */ class Share extends ActiveRecord { const string SCENARIO_UPDATE = 'update'; + /** * {@inheritdoc} */ @@ -35,7 +38,7 @@ class Share extends ActiveRecord { return [ [['file_relative_path', 'access_code'], 'required'], - [['sharer_id', 'status'], 'integer'], + [['sharer_id', 'status', 'dl_count'], 'integer'], [['creation_date'], 'safe'], [['file_relative_path'], 'string', 'max' => 255], [['access_code'], 'string', 'max' => 4], @@ -56,6 +59,8 @@ class Share extends ActiveRecord 'access_code' => '访问密码', 'creation_date' => '分享创建日期', 'status' => 'Status', + 'dl_count' => '下载次数', + ]; } @@ -68,8 +73,32 @@ class Share extends ActiveRecord { 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']); + + } }