Skip to content

Commit

Permalink
BlogCategoriesTable::beforeDelete() の残件対応
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Nov 1, 2023
1 parent 6a94cd7 commit 85643c0
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,28 +129,16 @@ public function validationDefault(Validator $validator): Validator
* 関連する記事データをカテゴリ無所属に変更し保存する
*
* @param boolean $cascade
* @return boolean
* @return void
*/
public function beforeDelete(EventInterface $event, EntityInterface $entity, \ArrayObject $options)
{
// TODO ucmitz 未実装
return true;
$ret = true;
if (!empty($this->data['BlogCategory']['id'])) {
$id = $this->data['BlogCategory']['id'];
$this->BlogPost->unBindModel(['belongsTo' => ['BlogCategory']]);
$datas = $this->BlogPost->find('all', ['conditions' => ['BlogPost.blog_category_id' => $id]]);
if ($datas) {
foreach ($datas as $data) {
$data['BlogPost']['blog_category_id'] = '';
$this->BlogPost->set($data);
if (!$this->BlogPost->save()) {
$ret = false;
}
}
}
}
return $ret;
$this->BlogPosts->find()
->where(['BlogPosts.blog_category_id' => $entity->id])
->each(function ($blogPost) {
$blogPost->blog_category_id = '';
$this->BlogPosts->save($blogPost);
});
}

/**
Expand Down

0 comments on commit 85643c0

Please sign in to comment.