diff --git a/src/Sylius/Bundle/CoreBundle/Doctrine/ORM/ProductRepository.php b/src/Sylius/Bundle/CoreBundle/Doctrine/ORM/ProductRepository.php index 5d668458280..718bda34abd 100644 --- a/src/Sylius/Bundle/CoreBundle/Doctrine/ORM/ProductRepository.php +++ b/src/Sylius/Bundle/CoreBundle/Doctrine/ORM/ProductRepository.php @@ -70,16 +70,17 @@ public function createShopListQueryBuilder( TaxonInterface $taxon, string $locale, array $sorting = [], - bool $reproduceTaxonomyTree = false + bool $includeAllDescendants = false ): QueryBuilder { $queryBuilder = $this->createQueryBuilder('o') ->addSelect('translation') ->innerJoin('o.translations', 'translation', 'WITH', 'translation.locale = :locale') - ->innerJoin('o.productTaxons', 'productTaxon'); + ->innerJoin('o.productTaxons', 'productTaxon') + ->innerJoin('productTaxon.taxon', 'taxon'); - if ($reproduceTaxonomyTree) { - $queryBuilder->orWhere('productTaxon.taxon = :taxon'); - $this->reproduceTaxonomyTree($taxon, $queryBuilder); + if ($includeAllDescendants) { + $queryBuilder->andWhere('taxon.left >= :taxonLeft'); + $queryBuilder->andWhere('taxon.right <= :taxonRight'); } else { $queryBuilder->andWhere('productTaxon.taxon = :taxon'); } @@ -88,7 +89,8 @@ public function createShopListQueryBuilder( ->andWhere(':channel MEMBER OF o.channels') ->andWhere('o.enabled = true') ->setParameter('locale', $locale) - ->setParameter('taxon', $taxon) + ->setParameter('taxonLeft', $taxon->getLeft()) + ->setParameter('taxonRight', $taxon->getRight()) ->setParameter('channel', $channel) ; @@ -180,20 +182,4 @@ public function findOneByCode(string $code): ?ProductInterface ->getOneOrNullResult() ; } - - private function reproduceTaxonomyTree(TaxonInterface $taxon, QueryBuilder $queryBuilder): void - { - if (!$taxon->hasChildren()) { - $parameter = 'taxonChild' . $taxon->getCode(); - - $queryBuilder->orWhere('productTaxon.taxon = :' . $parameter); - $queryBuilder->setParameter($parameter, $taxon); - - return; - } - - foreach ($taxon->getChildren() as $taxonChild) { - $this->reproduceTaxonomyTree($taxonChild, $queryBuilder); - } - } } diff --git a/src/Sylius/Bundle/ShopBundle/Resources/config/grids/product.yml b/src/Sylius/Bundle/ShopBundle/Resources/config/grids/product.yml index 772a98151c1..da7122b1c59 100644 --- a/src/Sylius/Bundle/ShopBundle/Resources/config/grids/product.yml +++ b/src/Sylius/Bundle/ShopBundle/Resources/config/grids/product.yml @@ -15,7 +15,7 @@ sylius_grid: taxon: "expr:notFoundOnNull(service('sylius.repository.taxon').findOneBySlug($slug, service('sylius.context.locale').getLocaleCode()))" locale: "expr:service('sylius.context.locale').getLocaleCode()" sorting: "expr:service('request_stack').getCurrentRequest().get('sorting', [])" - reproduceTaxonomyTree: true + includeAllDescendants: true sorting: position: asc limits: [9, 18, 27] diff --git a/src/Sylius/Component/Core/Repository/ProductRepositoryInterface.php b/src/Sylius/Component/Core/Repository/ProductRepositoryInterface.php index 9f48fcc765e..097abc2878c 100644 --- a/src/Sylius/Component/Core/Repository/ProductRepositoryInterface.php +++ b/src/Sylius/Component/Core/Repository/ProductRepositoryInterface.php @@ -34,7 +34,7 @@ public function createListQueryBuilder(string $locale, $taxonId = null): QueryBu * @param TaxonInterface $taxon * @param string $locale * @param array $sorting - * @param bool $reproduceTaxonomyTree + * @param bool $includeAllDescendants * * @return QueryBuilder */ @@ -43,7 +43,7 @@ public function createShopListQueryBuilder( TaxonInterface $taxon, string $locale, array $sorting = [], - bool $reproduceTaxonomyTree = false + bool $includeAllDescendants = false ): QueryBuilder; /**