public function actionDelete($id) { Yii::$app->response->format = \yii\web\Response::FORMAT_JSON; $items['files'] = []; if (($model = Media::findOne($id)) !== null) { try { $model->delete(); $items['files'][][$model->name] = true; $items['files'][][$id] = true; return $items; } catch (\Exception $e) { $items['files'][][$model->name] = false; $items['files'][][$id] = false; $items['files'][]['error'] = $e->getMessage(); return $items; } } else { $items['files'][][$id] = false; $items['files'][]['error'] = 'The requested image file does not exist.'; return $items; } }
/** * Delete a media album and all its media files */ protected function deleteMediaAlbum() { $files = Media::find()->where(['album_id' => $this->owner->{$this->attribute}])->asArray()->all(); foreach ($files as $file) { if (($model = Media::findOne($file['id'])) !== null) { $model->delete(); } } if (($album = MediaAlbum::findOne($this->owner->{$this->attribute})) !== null) { $album->delete(); $this->owner->{$this->attribute} = null; return true; } else { Yii::warning('Cannot delete Media Album.'); return false; } }
/** * Finds the Media model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Media the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Media::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }