Skip to content

Commit

Permalink
ブログ記事に対して追加でアソシエーションで繋げたデータが取得できない問題を改善
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Dec 11, 2024
1 parent 8522806 commit de0f110
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 50 deletions.
31 changes: 31 additions & 0 deletions plugins/bc-blog/src/Model/Table/BlogPostsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

}
52 changes: 2 additions & 50 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']));
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}

/**
* カテゴリ条件を生成する
*
Expand Down

0 comments on commit de0f110

Please sign in to comment.