From 1f61a94c1cc2fc43fe0ea450441061f5b6d19e43 Mon Sep 17 00:00:00 2001 From: noumo Date: Sun, 17 May 2015 22:53:49 +0300 Subject: [PATCH] fix relations after delete --- components/CategoryController.php | 5 +++++ modules/article/models/Item.php | 10 ++++++++++ modules/catalog/models/Item.php | 3 +-- modules/news/models/News.php | 10 ++++++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/components/CategoryController.php b/components/CategoryController.php index c49a841..685c9e5 100644 --- a/components/CategoryController.php +++ b/components/CategoryController.php @@ -2,6 +2,7 @@ namespace yii\easyii\components; use Yii; +use yii\db\ActiveRecord; use yii\easyii\behaviors\SortableModel; use yii\widgets\ActiveForm; use yii\web\UploadedFile; @@ -133,7 +134,11 @@ public function actionDelete($id) { $class = $this->categoryClass; if(($model = $class::findOne($id))){ + $children = $model->children()->all(); $model->deleteWithChildren(); + foreach($children as $child) { + $child->trigger(ActiveRecord::EVENT_AFTER_DELETE); + } } else { $this->error = Yii::t('easyii', 'Not found'); } diff --git a/modules/article/models/Item.php b/modules/article/models/Item.php index d7ea6b0..7899b66 100644 --- a/modules/article/models/Item.php +++ b/modules/article/models/Item.php @@ -4,6 +4,7 @@ use Yii; use yii\behaviors\SluggableBehavior; use yii\easyii\behaviors\SeoBehavior; +use yii\easyii\models\Photo; use yii\helpers\StringHelper; class Item extends \yii\easyii\components\ActiveRecord @@ -60,6 +61,11 @@ public function getCategory() return $this->hasOne(Category::className(), ['category_id' => 'category_id']); } + public function getPhotos() + { + return $this->hasMany(Photo::className(), ['item_id' => 'item_id'])->where(['class' => self::className()])->sort(); + } + public function beforeSave($insert) { if (parent::beforeSave($insert)) { @@ -83,5 +89,9 @@ public function afterDelete() if($this->image){ @unlink(Yii::getAlias('@webroot').$this->image); } + + foreach($this->getPhotos()->all() as $photo){ + $photo->delete(); + } } } \ No newline at end of file diff --git a/modules/catalog/models/Item.php b/modules/catalog/models/Item.php index af12d23..d550008 100644 --- a/modules/catalog/models/Item.php +++ b/modules/catalog/models/Item.php @@ -4,7 +4,6 @@ use Yii; use yii\behaviors\SluggableBehavior; use yii\easyii\behaviors\SeoBehavior; -use yii\easyii\behaviors\SortableModel; use yii\easyii\models\Photo; class Item extends \yii\easyii\components\ActiveRecord @@ -114,7 +113,7 @@ public function afterFind() public function getPhotos() { - return $this->hasMany(Photo::className(), ['item_id' => 'item_id'])->where(['class' => Item::className()])->sort(); + return $this->hasMany(Photo::className(), ['item_id' => 'item_id'])->where(['class' => self::className()])->sort(); } public function getCategory() diff --git a/modules/news/models/News.php b/modules/news/models/News.php index ece31a2..eeb1824 100644 --- a/modules/news/models/News.php +++ b/modules/news/models/News.php @@ -4,6 +4,7 @@ use Yii; use yii\behaviors\SluggableBehavior; use yii\easyii\behaviors\SeoBehavior; +use yii\easyii\models\Photo; use yii\helpers\StringHelper; class News extends \yii\easyii\components\ActiveRecord @@ -55,6 +56,11 @@ public function behaviors() ]; } + public function getPhotos() + { + return $this->hasMany(Photo::className(), ['item_id' => 'news_id'])->where(['class' => self::className()])->sort(); + } + public function beforeSave($insert) { if (parent::beforeSave($insert)) { @@ -77,5 +83,9 @@ public function afterDelete() if($this->image){ @unlink(Yii::getAlias('@webroot').$this->image); } + + foreach($this->getPhotos()->all() as $photo){ + $photo->delete(); + } } } \ No newline at end of file