示例#1
0
 /**
  * @param $id
  * @return string
  */
 public function run($id)
 {
     /** @var Image $file */
     $file = Image::findOne(['id' => $id]);
     $deleted = $file->delete();
     return $this->getResponse($file, $deleted);
 }
示例#2
0
 /**
  * @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);
 }
示例#3
0
 /**
  * @return string
  */
 public function run()
 {
     $file = new Image();
     return $file->upload() ? $this->getSuccessResponse($file) : $this->getErrorResponse($file);
 }
示例#4
0
 /**
  * @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;
 }