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 155be68 commit 0f67030
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 3 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 @@ -14,8 +14,11 @@
namespace Eccube\Tests\Repository;

use Eccube\Entity\CustomerFavoriteProduct;
use Eccube\Tests\EccubeTestCase;
use Eccube\Entity\Product;
use Eccube\Entity\ProductTag;
use Eccube\Repository\ProductRepository;
use Eccube\Repository\TagRepository;
use Eccube\Tests\EccubeTestCase;

/**
* ProductRepository test cases.
Expand All @@ -29,6 +32,11 @@ abstract class AbstractProductRepositoryTestCase extends EccubeTestCase
*/
protected $productRepository;

/**
* @var TagRepository
*/
protected $tagRepository;

/**
* {@inheritdoc}
*/
Expand All @@ -37,6 +45,7 @@ public function setUp()
parent::setUp();

$this->productRepository = $this->entityManager->getRepository(\Eccube\Entity\Product::class);
$this->tagRepository = $this->entityManager->getRepository(\Eccube\Entity\Tag::class);

$tables = [
'dtb_product_image',
Expand Down Expand Up @@ -67,4 +76,24 @@ protected function createFavorites($Customer)
}
$this->entityManager->flush();
}

/**
* 商品にタグをつける
*
* @param Product $Product
* @param array $tagIds
*/
protected function setProductTags(Product $Product, array $tagIds)
{
$Tags = $this->tagRepository->findBy(['id' => $tagIds]);
foreach ($Tags as $Tag) {
$ProductTag = new ProductTag();
$ProductTag
->setProduct($Product)
->setTag($Tag);
$Product->addProductTag($ProductTag);
$this->entityManager->persist($ProductTag);
}
$this->entityManager->flush();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -392,4 +392,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 0f67030

Please sign in to comment.