/** * Delete multiple existing Gallery model. * For ajax request will return json object * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page. * @return mixed */ public function actionPhotosDelete() { $request = Yii::$app->request; $photoIds = $request->post('ids'); // Array or selected records primary keys $photoModels = GalleryPhoto::findAll($photoIds); if (empty($photoModels)) { return null; } $galleryModel = $this->findModel($photoModels[0]->gallery_id); $dir = Yii::getAlias('@app/web/img/gallery/' . Translator::rus2translit($galleryModel->name)); foreach ($photoModels as $photo) { try { unlink($dir . '/' . $photo->name); unlink($dir . '/thumb/' . $photo->name); } catch (\Exception $e) { echo 'Не удалось удалить файл ' . $photo->name . ' - ' . $e->getMessage(); } } GalleryPhoto::deleteAll(['photo_id' => $photoIds]); if ($request->isAjax) { Yii::$app->response->format = Response::FORMAT_JSON; return true; } else { return $this->redirect(['index']); } }
public function getGalleryPhotos() { return $this->hasMany(GalleryPhoto::className(), ['gallery_id' => 'gallery_id'])->limit(4); }