From 85643c06fcf74e8babaf69234ca23dcb5bc78757 Mon Sep 17 00:00:00 2001 From: ryuring Date: Wed, 1 Nov 2023 20:43:09 +0900 Subject: [PATCH] =?UTF-8?q?BlogCategoriesTable::beforeDelete()=20=E3=81=AE?= =?UTF-8?q?=E6=AE=8B=E4=BB=B6=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Model/Table/BlogCategoriesTable.php | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php index 01c00a8594..5144b4ec42 100755 --- a/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogCategoriesTable.php @@ -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); + }); } /**