Inheritance: extends yii\db\ActiveRecord, use trait nemmo\attachments\ModuleTrait
示例#1
0
 public function testValidate()
 {
     $file = new File();
     $this->assertFalse($file->validate());
     $this->assertArrayHasKey('name', $file->errors);
     $this->assertArrayHasKey('model', $file->errors);
     $this->assertArrayHasKey('itemId', $file->errors);
     $this->assertArrayHasKey('hash', $file->errors);
     $this->assertArrayHasKey('size', $file->errors);
     $this->assertArrayHasKey('type', $file->errors);
     $this->assertArrayHasKey('mime', $file->errors);
 }
示例#2
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();
 }
 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}");
 }
示例#4
0
 public function detachFile($id)
 {
     /** @var File $file */
     $file = File::findOne(['id' => $id]);
     if (empty($file)) {
         return false;
     }
     $filePath = $this->getFilesDirPath($file->hash) . DIRECTORY_SEPARATOR . $file->hash . '.' . $file->type;
     // this is the important part of the override.
     // the original methods doesn't check for file_exists to be
     return file_exists($filePath) ? unlink($filePath) && $file->delete() : $file->delete();
 }
示例#5
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();
 }