Example #1
0
 public function detachFile($id)
 {
     $file = File::findOne(['id' => $id]);
     $filePath = $this->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;
     unlink($filePath);
     $file->delete();
 }
Example #2
0
 /**
  * TODO закешировать
  * @return \yii\db\ActiveQuery
  */
 public function getAttachedFiles()
 {
     return $this->hasMany(File::className(), ['itemId' => 'id'])->andWhere(['model' => \yii\helpers\StringHelper::basename(get_class($this))]);
 }
Example #3
0
 /**
  * @return File[]
  * @throws \Exception
  */
 public function getFiles()
 {
     $fileQuery = File::find()->where(['itemId' => $this->owner->id, 'model' => $this->getModule()->getShortClass($this->owner)]);
     $fileQuery->orderBy(['id' => SORT_ASC]);
     return $fileQuery->all();
 }
Example #4
0
 public function actionDownload($id)
 {
     $file = File::findOne(['id' => $id]);
     $filePath = $this->getModule()->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;
     return \Yii::$app->response->sendFile($filePath, "{$file->name}.{$file->type}");
 }