/** * @param $id * @return string */ public function run($id) { /** @var Image $file */ $file = Image::findOne(['id' => $id]); $deleted = $file->delete(); return $this->getResponse($file, $deleted); }
/** * @param $id * @return string * @throws NotFoundHttpException */ public function actionDelete($id) { /** @var Image $image */ $image = Image::findOne(['id' => $id]); if (!$image) { throw new NotFoundHttpException(); } $image->delete(); return Json::encode(1); }
/** * @return string */ public function run() { $file = new Image(); return $file->upload() ? $this->getSuccessResponse($file) : $this->getErrorResponse($file); }
/** * @param $path * @param bool|false $primary * @return bool * @throws \yii\db\Exception */ public function attachImage($path, $primary = false) { $image = new Image(); if (!$image->uploadFrom($path)) { return false; } $itemId = $this->owner->primaryKey; $model = $this->getModelShortName(); $query = (new Query())->from($this->junctionTable)->where(['model' => $model, 'item_id' => $itemId]); $sort = $primary ? $query->min('sort') - 1 : $query->max('sort') + 1; $this->owner->getDb()->createCommand()->insert($this->junctionTable, ['file_id' => $image->id, 'model' => $model, 'item_id' => $itemId, 'sort' => $sort])->execute(); return true; }