From de0f110e3d8f10d346018270b36c1dd54f5a75c0 Mon Sep 17 00:00:00 2001 From: ryuring Date: Wed, 11 Dec 2024 16:01:16 +0900 Subject: [PATCH] =?UTF-8?q?=E3=83=96=E3=83=AD=E3=82=B0=E8=A8=98=E4=BA=8B?= =?UTF-8?q?=E3=81=AB=E5=AF=BE=E3=81=97=E3=81=A6=E8=BF=BD=E5=8A=A0=E3=81=A7?= =?UTF-8?q?=E3=82=A2=E3=82=BD=E3=82=B7=E3=82=A8=E3=83=BC=E3=82=B7=E3=83=A7?= =?UTF-8?q?=E3=83=B3=E3=81=A7=E7=B9=8B=E3=81=92=E3=81=9F=E3=83=87=E3=83=BC?= =?UTF-8?q?=E3=82=BF=E3=81=8C=E5=8F=96=E5=BE=97=E3=81=A7=E3=81=8D=E3=81=AA?= =?UTF-8?q?=E3=81=84=E5=95=8F=E9=A1=8C=E3=82=92=E6=94=B9=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Model/Table/BlogPostsTable.php | 31 +++++++++++ .../bc-blog/src/Service/BlogPostsService.php | 52 +------------------ 2 files changed, 33 insertions(+), 50 deletions(-) diff --git a/plugins/bc-blog/src/Model/Table/BlogPostsTable.php b/plugins/bc-blog/src/Model/Table/BlogPostsTable.php index afc55ef148..0654861ecd 100755 --- a/plugins/bc-blog/src/Model/Table/BlogPostsTable.php +++ b/plugins/bc-blog/src/Model/Table/BlogPostsTable.php @@ -20,6 +20,7 @@ use BaserCore\Model\Table\UsersTable; use BaserCore\Utility\BcUtil; use BcBlog\Model\Entity\BlogPost; +use Cake\Collection\CollectionInterface; use Cake\Core\Plugin; use Cake\Database\Driver\Mysql; use Cake\Database\Driver\Postgres; @@ -860,4 +861,34 @@ public function getPublishByNo(int $blogContentId, mixed $no, bool $preview = fa return $entity; } + /** + * find all + * @param Query $query + * @param array $options + * @return Query + * @checked + * @noTodo + */ + public function findAll(Query $query, array $options = []): Query + { + if($options['draft'] !== false) return $query; + return $query->formatResults(function(CollectionInterface $results) { + return $results->map([$this, 'excludeDraftFields']); + }); + } + + /** + * 草稿データを除外する + * @param EntityInterface $entity + * @return EntityInterface + * @checked + * @noTodo + */ + public function excludeDraftFields(EntityInterface $entity) + { + unset($entity->content_draft); + unset($entity->detail_draft); + return $entity; + } + } diff --git a/plugins/bc-blog/src/Service/BlogPostsService.php b/plugins/bc-blog/src/Service/BlogPostsService.php index 5858424729..787c820d23 100755 --- a/plugins/bc-blog/src/Service/BlogPostsService.php +++ b/plugins/bc-blog/src/Service/BlogPostsService.php @@ -155,7 +155,8 @@ public function getIndex(array $queryParams = []): Query if ($options['id'] || $options['no']) $options['contain'][] = 'BlogComments'; if ($options['contain'] == null) $options['contain'] = []; - $query = $this->BlogPosts->find()->contain($options['contain']); + $query = $this->BlogPosts->find('all', ['draft' => $options['draft']]) + ->contain($options['contain']); if ($options['order']) { $query->orderBy($this->createOrder($options['order'], $options['direction'])); @@ -262,10 +263,6 @@ protected function createIndexConditions(Query $query, array $params) $conditions = $this->BlogPosts->getConditionAllowPublish(); $conditions = array_merge($conditions, $this->BlogPosts->BlogContents->Contents->getConditionAllowPublish()); $query->contain(['BlogContents' => ['Contents']]); - if($params['draft'] === false) { - $query->selectAllExcept($this->BlogPosts, ['content_draft', 'detail_draft']); - $query = $this->selectContains($query); - } } elseif ((string)$params['status'] === '0') { $conditions = ['BlogPosts.status' => false]; } else { @@ -350,51 +347,6 @@ protected function createIndexConditions(Query $query, array $params) return $query->where($conditions); } - /** - * Contains を select を前提として適用する - * select を利用した場合、関連テーブルのカラムを指定しないと、取得できないため - * @param Query $query - * @param array $contains - * @return Query - * @noTodo - * @checked - */ - public function selectContains(Query $query, array $contains = []) - { - if(!$contains) $contains = $query->getContain(); - if(isset($contains['BlogContents'])) { - $query->contain(['BlogContents' => function($q) { - return $q->select($this->BlogPosts->BlogContents); - }]); - } - if(isset($contains['BlogContents']['Contents'])) { - $query->contain(['BlogContents.Contents' => function($q) { - return $q->select($this->BlogPosts->BlogContents->Contents); - }]); - } - if(isset($contains['Users'])) { - $query->contain(['Users' => function($q) { - return $q->select($this->BlogPosts->Users); - }]); - } - if(isset($contains['BlogComments'])) { - $query->contain(['BlogComments' => function($q) { - return $q->select($this->BlogPosts->BlogComments); - }]); - } - if(isset($contains['BlogCategories'])) { - $query->contain(['BlogCategories' => function($q) { - return $q->select($this->BlogPosts->BlogCategories); - }]); - } - if(isset($contains['BlogTags'])) { - $query->contain(['BlogTags' => function($q) { - return $q->select($this->BlogPosts->BlogTags); - }]); - } - return $query; - } - /** * カテゴリ条件を生成する *