Skip to content

Commit

Permalink
タグ検索改善
Browse files Browse the repository at this point in the history
  • Loading branch information
ryuring committed Mar 27, 2024
1 parent 917515f commit 95f7061
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions plugins/bc-blog/src/Service/BlogPostsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ protected function createIndexConditions(Query $query, array $params)
}
// タグ名
if ($params['tag']) {
$conditions = $this->createTagCondition($conditions, $params['tag']);
$query = $this->createTagCondition($query, $params['tag']);
}
// 年月日
if ($params['year'] || $params['month'] || $params['day']) {
Expand Down Expand Up @@ -421,23 +421,16 @@ public function createCategoryCondition(
* @noTodo
* @unitTest
*/
public function createTagCondition($conditions, $tag)
public function createTagCondition($query, $tag)
{
if (!is_array($tag)) $tag = [$tag];
foreach($tag as $key => $value) {
$tag[$key] = rawurldecode($value);
}
$tags = $this->BlogPosts->BlogTags->find()
->where(['BlogTags.name IN' => $tag])
->contain(['BlogPosts'])
->all()->toArray();
$postIds = Hash::extract($tags, '{n}.blog_posts.{n}.id');
if ($postIds) {
$conditions['BlogPosts.id IN'] = $postIds;
} else {
$conditions['BlogPosts.id IS'] = null;
}
return $conditions;
$query->matching('BlogTags', function($q) use ($tag) {
return $q->where(['BlogTags.name IN' => $tag]);
});
return $query;
}

/**
Expand Down

0 comments on commit 95f7061

Please sign in to comment.