protected function getModel($id = null) { $model = null; if ($id) { $model = File::findOne(["id" => $id]); } else { $model = new File(); } return $model; }
public function getMainProductImage() { $file = $this->hasMany(File::className(), ['id' => 'id_File'])->viaTable('rel_File', ['_entity_id' => 'id'], function ($query) { $query->orderBy(['order' => SORT_DESC]); })->one(); if (!$file) { $file = File::getSystemFile(File::EMPTY_IMAGE); } return $file; }
public function actionUpload() { include __DIR__ . "./../uploader/Uploader.php"; $upload_dir = \Yii::$app->getModule("file")->getStorageDir(); $uploader = new \FileUpload('uploadfile'); $fileModel = new File(); $realName = $_GET["uploadfile"]; // Handle the upload $isUplaoded = $uploader->handleUpload($upload_dir); $result = false; $errorMsg = ''; if ($isUplaoded) { $fileModel = new File(); $fileModel->setAttribute("real_name", $realName); $fileModel->setAttribute("name_on_server", $uploader->getSavedFileName()); $fileModel->setAttribute("size", $uploader->getFileSize()); if (!$fileModel->save()) { $uploader->rollBack(); $errorMsg = "Entity save error"; } else { if (!empty($_GET["relateTo"])) { if (!$fileModel->linkWith($_GET["relateTo"])) { $uploader->rollBack(); $fileModel->delete(); $errorMsg = "Entity link error"; } else { $result = true; } } else { $result = true; } } } else { $errorMsg = $uploader->getErrorMsg(); } if ($result) { echo json_encode(array('success' => true)); } else { exit(json_encode(['success' => false, 'msg' => $errorMsg])); } }
/** * Get system file * * @param string $systemKey Unique system key * * @return ActiveQueryInterface the newly created [[ActiveQueryInterface|ActiveQuery]] instance. */ public static function getSystemFile($systemKey) { return File::findOne(['system_key' => $systemKey]); }