Inheritance: extends yii\db\ActiveRecord, use trait nemmo\attachments\ModuleTrait
コード例 #1
0
ファイル: FileTest.php プロジェクト: nemmo/yii2-attachments
 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();
 }
コード例 #3
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}");
 }
コード例 #4
0
ファイル: Module.php プロジェクト: nemmo/yii2-attachments
 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();
 }