Skip to content

Commit

Permalink
Merge pull request #2388 from dpfaffenbauer/issue/optimize-recursive-…
Browse files Browse the repository at this point in the history
…checker

[Core] Optimize Category recursive and Product Variants Condition Checker
  • Loading branch information
dpfaffenbauer authored Oct 10, 2023
2 parents 15f8d24 + e153c96 commit 96a59f4
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use CoreShop\Component\Core\Repository\CategoryRepositoryInterface;
use CoreShop\Component\Product\Model\CategoryInterface;
use CoreShop\Component\Store\Model\StoreInterface;
use Doctrine\DBAL\Connection;
use Pimcore\Model\DataObject\Listing;

class CategoryRepository extends BaseCategoryRepository implements CategoryRepositoryInterface
Expand Down Expand Up @@ -55,6 +56,41 @@ public function findChildCategoriesForStore(CategoryInterface $category, StoreIn
return $list->getObjects();
}

public function findRecursiveChildCategoryIdsForStoreByCategories(array $categories, StoreInterface $store): array
{
$list = $this->getList();
$dao = $list->getDao();

/** @psalm-suppress InternalMethod */
$query = "
SELECT oo_id as id FROM (
SELECT CONCAT(o_path, o_key) as realFullPath FROM objects WHERE o_id IN (:categories)
) as categories
INNER JOIN ".$dao->getTableName()." variants ON variants.o_path LIKE CONCAT(categories.realFullPath, '/%')
";

$params = [
'categories' => $categories,
];
$paramTypes = [
'categories' => Connection::PARAM_STR_ARRAY,
];

$resultCategories = $this->connection->fetchAllAssociative($query, $params, $paramTypes);

$childs = [];

foreach ($categories as $categoryId) {
$childs[$categoryId] = true;
}

foreach ($resultCategories as $result) {
$childs[$result['id']] = true;
}

return array_keys($childs);
}

public function findRecursiveChildCategoryIdsForStore(CategoryInterface $category, StoreInterface $store): array
{
$list = $this->getList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use CoreShop\Component\Core\Repository\ProductRepositoryInterface;
use CoreShop\Component\Core\Repository\ProductVariantRepositoryInterface;
use CoreShop\Component\Store\Model\StoreInterface;
use Doctrine\DBAL\Connection;
use Pimcore\Model\DataObject\AbstractObject;
use Pimcore\Model\DataObject\Listing;
use Symfony\Component\OptionsResolver\OptionsResolver;
Expand Down Expand Up @@ -55,6 +56,42 @@ public function findAllVariants(ProductInterface $product, bool $recursive = tru
return $list->getObjects();
}

public function findRecursiveVariantIdsForProductAndStoreByProducts(array $products, StoreInterface $store): array
{
$list = $this->getList();
$dao = $list->getDao();

/** @psalm-suppress InternalMethod */
$query = "
SELECT oo_id as id FROM (
SELECT CONCAT(o_path, o_key) as realFullPath FROM objects WHERE o_id IN (:products)
) as products
INNER JOIN ".$dao->getTableName()." variants ON variants.o_path LIKE CONCAT(products.realFullPath, '/%')
";

$params = [
'products' => $products,
];
$paramTypes = [
'products' => Connection::PARAM_STR_ARRAY,
];

$resultProducts = $this->connection->fetchAllAssociative($query, $params, $paramTypes);

$variantIds = [];

foreach ($products as $productId) {
$variantIds[$productId] = true;
}

foreach ($resultProducts as $result) {
$variantIds[$result['id']] = true;
}

return array_keys($variantIds);
}


public function findRecursiveVariantIdsForProductAndStore(ProductInterface $product, StoreInterface $store): array
{
$list = $this->getList();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public function findChildCategoriesForStore(CategoryInterface $category, StoreIn
*/
public function findRecursiveChildCategoryIdsForStore(CategoryInterface $category, StoreInterface $store): array;

/**
* @return int[]
*/
public function findRecursiveChildCategoryIdsForStoreByCategories(array $categories, StoreInterface $store): array;

/**
* @return CategoryInterface[]
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@ interface ProductVariantRepositoryInterface extends BaseProductRepositoryInterfa
public function findAllVariants(ProductInterface $product, bool $recursive = true): array;

public function findRecursiveVariantIdsForProductAndStore(ProductInterface $product, StoreInterface $store): array|\Pimcore\Model\DataObject\Listing;

public function findRecursiveVariantIdsForProductAndStoreByProducts(array $products, StoreInterface $store): array;
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,7 @@ protected function getCategoriesToCheck(array $categories, StoreInterface $store
$categoryIdsToCheck = $categories;

if ($recursive) {
foreach ($categories as $categoryId) {
$category = $this->categoryRepository->find($categoryId);

if (!$category instanceof CategoryInterface) {
continue;
}

$subCategories = $this->categoryRepository->findRecursiveChildCategoryIdsForStore($category, $store);

foreach ($subCategories as $child) {
if (!in_array($child, $categoryIdsToCheck)) {
$categoryIdsToCheck[] = $child;
}
}
}
$categoryIdsToCheck = $this->categoryRepository->findRecursiveChildCategoryIdsForStoreByCategories($categories, $store);
}

return $categoryIdsToCheck;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,7 @@ protected function getProductsToCheck(array $products, StoreInterface $store, bo
$productIdsToCheck = $products;

if ($includeVariants) {
foreach ($products as $productId) {
$product = $this->productRepository->find($productId);

if (!$product instanceof ProductInterface) {
continue;
}
$variants = $this->productRepository->findRecursiveVariantIdsForProductAndStore($product, $store);

foreach ($variants as $variant) {
if (!in_array($variant, $productIdsToCheck)) {
$productIdsToCheck[] = $variant;
}
}
}
$productIdsToCheck = $this->productRepository->findRecursiveVariantIdsForProductAndStoreByProducts($products, $store);
}

return $productIdsToCheck;
Expand Down

0 comments on commit 96a59f4

Please sign in to comment.