Skip to content

Commit

Permalink
商品のタグ名による検索
Browse files Browse the repository at this point in the history
  • Loading branch information
h.matsuo committed Nov 26, 2021
1 parent c38715c commit c248bdf
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Eccube/Repository/ProductRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,18 @@ public function getQueryBuilderBySearchData($searchData)
$qb
->andWhere(sprintf('NORMALIZE(p.name) LIKE NORMALIZE(:%s) OR
NORMALIZE(p.search_word) LIKE NORMALIZE(:%s) OR
EXISTS (SELECT wpc%d FROM \Eccube\Entity\ProductClass wpc%d WHERE p = wpc%d.Product AND NORMALIZE(wpc%d.code) LIKE NORMALIZE(:%s))',
$key, $key, $index, $index, $index, $index, $key))
EXISTS (SELECT wpc%d FROM \Eccube\Entity\ProductClass wpc%d WHERE p = wpc%d.Product AND NORMALIZE(wpc%d.code) LIKE NORMALIZE(:%s)) OR
EXISTS (
SELECT wpt%d from \Eccube\Entity\ProductTag wpt%d
WHERE p = wpt%d.Product AND
wpt%d.Tag IN (
SELECT wt%d FROM \Eccube\Entity\Tag wt%d
WHERE NORMALIZE(wt%d.name) LIKE NORMALIZE(:%s)
)
)',
$key, $key,
$index, $index, $index, $index, $key,
$index, $index, $index, $index, $index, $index, $index, $key))
->setParameter($key, '%'.$keyword.'%');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,4 +393,51 @@ public function test300ProductsList()
}
$this->verify();
}

public function testTagSearch()
{
// データの事前準備
// * 商品A に タグ 1 を設定
// * 商品B に タグ 2, 2 を設定
$Products = $this->productRepository->findAll();
$Products[1]->setName('りんご');
$this->setProductTags($Products[1], [1]);
$this->setProductTags($Products[2], [1, 2]);
$this->entityManager->flush();

// タグ 1 で検索
$this->searchData = [
'name' => '新商品',
];
$this->scenario();
$this->assertCount(2, $this->Results);

// タグ 2 で検索
$this->searchData = [
'name' => 'おすすめ商品',
];
$this->scenario();
$this->assertCount(1, $this->Results);

// タグ 1 and タグ 2 で検索
$this->searchData = [
'name' => '新商品 おすすめ商品',
];
$this->scenario();
$this->assertCount(1, $this->Results);

// タグ 1 and 商品名 で検索
$this->searchData = [
'name' => '新商品 りんご',
];
$this->scenario();
$this->assertCount(1, $this->Results);

// タグ 3 で検索
$this->searchData = [
'name' => '限定品',
];
$this->scenario();
$this->assertCount(0, $this->Results);
}
}

0 comments on commit c248bdf

Please sign in to comment.