Skip to content

Commit

Permalink
fix relations after delete
Browse files Browse the repository at this point in the history
  • Loading branch information
noumo committed May 17, 2015
1 parent 0e0e51b commit 1f61a94
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
5 changes: 5 additions & 0 deletions components/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down
10 changes: 10 additions & 0 deletions modules/article/models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -83,5 +89,9 @@ public function afterDelete()
if($this->image){
@unlink(Yii::getAlias('@webroot').$this->image);
}

foreach($this->getPhotos()->all() as $photo){
$photo->delete();
}
}
}
3 changes: 1 addition & 2 deletions modules/catalog/models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
10 changes: 10 additions & 0 deletions modules/news/models/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand All @@ -77,5 +83,9 @@ public function afterDelete()
if($this->image){
@unlink(Yii::getAlias('@webroot').$this->image);
}

foreach($this->getPhotos()->all() as $photo){
$photo->delete();
}
}
}

0 comments on commit 1f61a94

Please sign in to comment.