Browse Source

Optimisation nombre requêtes

packProduct
Guillaume 3 years ago
parent
commit
e41a2e3923
9 changed files with 110 additions and 37 deletions
  1. +20
    -3
      Builder/Order/OrderShopBuilder.php
  2. +2
    -1
      Controller/ControllerTrait.php
  3. +3
    -0
      Repository/Product/ProductCategoryRepositoryQuery.php
  4. +21
    -7
      Repository/Product/ProductFamilyRepositoryQuery.php
  5. +6
    -6
      Repository/Product/ProductFamilyStore.php
  6. +2
    -2
      Repository/Product/ProductRepositoryQuery.php
  7. +40
    -7
      Repository/Reduction/ReductionCatalogRepositoryQuery.php
  8. +14
    -10
      Resolver/SectionResolver.php
  9. +2
    -1
      Twig/StoreTwigExtension.php

+ 20
- 3
Builder/Order/OrderShopBuilder.php View File

use Lc\SovBundle\Translation\FlashBagTranslator; use Lc\SovBundle\Translation\FlashBagTranslator;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
use Symfony\Contracts\Cache\TagAwareCacheInterface;


class OrderShopBuilder class OrderShopBuilder
{ {
return $orderShop; return $orderShop;
} }



protected array $cacheCartCurrentBySection = [];

public function createIfNotExist( public function createIfNotExist(
SectionInterface $section, SectionInterface $section,
UserInterface $user = null, UserInterface $user = null,
VisitorInterface $visitor = null
VisitorInterface $visitor = null,
bool $cache = false
): OrderShopInterface { ): OrderShopInterface {

// cache
$cacheIdCartCurrent = 'cart_current_'.$section->getId();
if($cache && isset($this->cacheCartCurrentBySection[$cacheIdCartCurrent])) {
return $this->cacheCartCurrentBySection[$cacheIdCartCurrent];
}

$this->orderShopStore->setSection($section); $this->orderShopStore->setSection($section);


$cartUser = $this->orderShopStore->getOneCartCurrent($user); $cartUser = $this->orderShopStore->getOneCartCurrent($user);
$cart = $this->create($section, $user, $visitor); $cart = $this->create($section, $user, $visitor);
} }


// @TODO : obligé de faire ça sinon le panier ne se met pas à jour quand on ajoute des produits. Pourquoi ?
$this->entityManager->refresh($cart);
// cache
$this->cacheCartCurrentBySection[$cacheIdCartCurrent] = $cart;


return $cart; return $cart;

// @TODO : obligé de faire ça sinon le panier ne se met pas à jour quand on ajoute des produits. Pourquoi ?
//$this->entityManager->refresh($cart);
} }


public function setOrderStatus( public function setOrderStatus(

+ 2
- 1
Controller/ControllerTrait.php View File

return $this->getOrderShopContainer()->getBuilder()->createIfNotExist( return $this->getOrderShopContainer()->getBuilder()->createIfNotExist(
$this->getSectionCurrent(), $this->getSectionCurrent(),
$this->getUserCurrent(), $this->getUserCurrent(),
$this->getVisitorCurrent()
$this->getVisitorCurrent(),
true
); );
} }



+ 3
- 0
Repository/Product/ProductCategoryRepositoryQuery.php View File



return $this; return $this;
} }



public function hasProductFamilyOnline(): self public function hasProductFamilyOnline(): self
{ {
$this->joinProductFamilies(); $this->joinProductFamilies();

+ 21
- 7
Repository/Product/ProductFamilyRepositoryQuery.php View File



class ProductFamilyRepositoryQuery extends AbstractRepositoryQuery class ProductFamilyRepositoryQuery extends AbstractRepositoryQuery
{ {
//use SectionRepositoryQueryTrait;

protected bool $isJoinProductCategories = false; protected bool $isJoinProductCategories = false;
protected bool $isJoinProductFamilySectionProperties = false; protected bool $isJoinProductFamilySectionProperties = false;
protected bool $isJoinProducts = false; protected bool $isJoinProducts = false;
protected bool $isJoinQualityLabels = false;


public function __construct(ProductFamilyRepository $repository, PaginatorInterface $paginator) public function __construct(ProductFamilyRepository $repository, PaginatorInterface $paginator)
{ {
return $this; return $this;
} }


public function joinQualityLabels(bool $addSelect = true): self
{
if (!$this->isJoinQualityLabels) {
$this->isJoinQualityLabels = true;

$this->leftJoin('.qualityLabels', 'pfql');
if ($addSelect) {
$this->addSelect('pfql');
}
}
return $this;
}

public function filterBySection(SectionInterface $section,bool $addSelectProductFamilySectionProperties = true) public function filterBySection(SectionInterface $section,bool $addSelectProductFamilySectionProperties = true)
{ {
$this->joinProductFamilySectionProperties($addSelectProductFamilySectionProperties); $this->joinProductFamilySectionProperties($addSelectProductFamilySectionProperties);
return $this->andWhereEqual('supplier', $supplier); return $this->andWhereEqual('supplier', $supplier);
} }


public function joinProductCategories(): self
public function joinProductCategories(bool $addSelect = false): self
{ {
if (!$this->isJoinProductCategories) { if (!$this->isJoinProductCategories) {
$this->isJoinProductCategories = true; $this->isJoinProductCategories = true;


return $this return $this
->leftJoin('.productCategories', 'cat');
//$query->addSelect('cat') ; ???
->leftJoin('.productCategories', 'cat')
->addSelect('cat')
;
} }
return $this; return $this;
} }
$this->isJoinProducts = true; $this->isJoinProducts = true;


return $this return $this
->innerJoin('.products', 'pfp');
// $query->addSelect('pfp') ; ?????
->innerJoin('.products', 'pfp')
->addSelect('pfp')
;
} }
return $this; return $this;
} }

+ 6
- 6
Repository/Product/ProductFamilyStore.php View File



public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface public function filtersDefault(RepositoryQueryInterface $query): RepositoryQueryInterface
{ {
if($this->section) {
$query->filterBySection($this->section);
}


if(isset($this->merchant) && $this->merchant) {
$query->filterByMerchantViaSection($this->merchant);
}
$this->addFilterBySectionOptionnal($query);
$this->addFilterByMerchantViaSectionOptionnal($query);


return $query; return $query;
} }
{ {
$query->joinProductCategories(); $query->joinProductCategories();
$query->joinProducts(); $query->joinProducts();
$query->joinQualityLabels();

return $query; return $query;
} }




if (($onlyOnDiscount && $productFamily->getReductionCatalog()) || !$onlyOnDiscount) { if (($onlyOnDiscount && $productFamily->getReductionCatalog()) || !$onlyOnDiscount) {
if ($organizeByParentCategory) { if ($organizeByParentCategory) {

$productCategories = $productFamily->getProductCategories(); $productCategories = $productFamily->getProductCategories();

if ($productCategories && count($productCategories) > 0) { if ($productCategories && count($productCategories) > 0) {
$parentCategory = $productCategories[0]->getParentCategory(); $parentCategory = $productCategories[0]->getParentCategory();
if ($this->productCategorySolver->isDisplay($parentCategory, $user)) { if ($this->productCategorySolver->isDisplay($parentCategory, $user)) {

+ 2
- 2
Repository/Product/ProductRepositoryQuery.php View File

$this->isJoinProductFamily = true; $this->isJoinProductFamily = true;


return $this return $this
->innerJoin('.productFamily', 'pf');
//$qb->addSelect('pf'); ???
->innerJoin('.productFamily', 'pf')
->addSelect('pf');
} }
return $this; return $this;
} }

+ 40
- 7
Repository/Reduction/ReductionCatalogRepositoryQuery.php View File

protected bool $isJoinProductFamilies = false; protected bool $isJoinProductFamilies = false;
protected bool $isJoinProductFamily = false; protected bool $isJoinProductFamily = false;
protected bool $isJoinProductCategories = false; protected bool $isJoinProductCategories = false;
protected bool $isJoinUsers = false;
protected bool $isJoinGroupUsers = false;

use SectionRepositoryQueryTrait; use SectionRepositoryQueryTrait;


public function __construct(ReductionCatalogRepository $repository, PaginatorInterface $paginator) public function __construct(ReductionCatalogRepository $repository, PaginatorInterface $paginator)
parent::__construct($repository, 'r', $paginator); parent::__construct($repository, 'r', $paginator);
} }


public function joinUsers()
{
if (!$this->isJoinUsers) {
$this->isJoinUsers = true;

return $this
->leftJoin('.users', 'pf_users')
->addSelect('pf_users') ;
}
return $this;
}

public function joinGroupUsers()
{
if (!$this->isJoinGroupUsers) {
$this->isJoinGroupUsers = true;

return $this
->leftJoin('.groupUsers', 'pf_groupusers')
->addSelect('pf_groupusers') ;
}
return $this;
}

public function joinProductFamilies() public function joinProductFamilies()
{ {
if (!$this->isJoinProductFamilies) { if (!$this->isJoinProductFamilies) {
$this->isJoinProductFamilies = true; $this->isJoinProductFamilies = true;


return $this return $this
->leftJoin('.productFamilies', 'pfs');
//->addSelect('pfs') ;
->leftJoin('.productFamilies', 'pfs')
->addSelect('pfs') ;
} }
return $this; return $this;
} }
$this->isJoinProductFamily = true; $this->isJoinProductFamily = true;


return $this return $this
->leftJoin('.productFamily', 'pf');
//->addSelect('pf') ;
->leftJoin('.productFamily', 'pf')
->addSelect('pf') ;
} }
return $this; return $this;
} }
$this->isJoinProductCategories = true; $this->isJoinProductCategories = true;


return $this return $this
->leftJoin('.productCategories', 'pcs');
//->addSelect('pcs') ;
->leftJoin('.productCategories', 'pcs')
->addSelect('pcs') ;
} }
return $this; return $this;
} }



public function filterProductFamily(ProductFamilyInterface $productFamily) public function filterProductFamily(ProductFamilyInterface $productFamily)
{ {
return $this return $this


public function filterConditionUser(UserInterface $user = null) public function filterConditionUser(UserInterface $user = null)
{ {
$this->joinUsers();

if ($user) { if ($user) {
return $this return $this
->andWhere(':user MEMBER OF .users OR .users is empty') ->andWhere(':user MEMBER OF .users OR .users is empty')


public function filterConditionGroupUser(UserInterface $user = null) public function filterConditionGroupUser(UserInterface $user = null)
{ {
$this->joinGroupUsers();

if ($user) { if ($user) {
return $this return $this
->andWhere(':groupUser MEMBER OF .groupUsers OR .groupUsers is empty') ->andWhere(':groupUser MEMBER OF .groupUsers OR .groupUsers is empty')
{ {
$this->joinProductFamilies(); $this->joinProductFamilies();
$this->joinProductFamily(); $this->joinProductFamily();

return $this return $this
->andWhere(':productFamilies MEMBER OF .productFamilies OR .productFamilies is empty') ->andWhere(':productFamilies MEMBER OF .productFamilies OR .productFamilies is empty')
->setParameter('productFamilies', $productFamilies); ->setParameter('productFamilies', $productFamilies);
{ {
$this->joinProductFamilies(); $this->joinProductFamilies();
$this->joinProductFamily(); $this->joinProductFamily();

return $this return $this
->andWhere(':productFamily MEMBER OF .productFamilies OR .productFamilies is empty') ->andWhere(':productFamily MEMBER OF .productFamilies OR .productFamilies is empty')
->setParameter('productFamily', $productFamily); ->setParameter('productFamily', $productFamily);
public function filterConditionProductCategories(array $productCategories) public function filterConditionProductCategories(array $productCategories)
{ {
$this->joinProductCategories(); $this->joinProductCategories();

return $this return $this
->andWhere(':productCategory MEMBER OF .productCategories OR .productCategories is empty') ->andWhere(':productCategory MEMBER OF .productCategories OR .productCategories is empty')
->setParameter('productCategory', $productCategories); ->setParameter('productCategory', $productCategories);

+ 14
- 10
Resolver/SectionResolver.php View File

} }
} // front } // front
else { else {
$merchantCurrent = $this->merchantResolver->getCurrent();
$sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
$sectionCurrent = null;
$sectionDefault = $sectionStore->getOneDefault();

if (isset($requestAttributesArray['section'])) {
$sectionCurrent = $sectionStore
->setMerchant($merchantCurrent)
->getOneBySlug($requestAttributesArray['section']);
if($this->section === null) {
$merchantCurrent = $this->merchantResolver->getCurrent();
$sectionStore = $this->sectionStore->setMerchant($merchantCurrent);
$sectionCurrent = null;
$sectionDefault = $sectionStore->getOneDefault();

if (isset($requestAttributesArray['section'])) {
$sectionCurrent = $sectionStore
->setMerchant($merchantCurrent)
->getOneBySlug($requestAttributesArray['section']);
}

$this->section = $sectionCurrent ?: $sectionDefault;
} }


return $sectionCurrent ?: $sectionDefault;
return $this->section;
} }
} }



+ 2
- 1
Twig/StoreTwigExtension.php View File

return $this->orderShopBuilder->createIfNotExist( return $this->orderShopBuilder->createIfNotExist(
$this->sectionResolver->getCurrent(), $this->sectionResolver->getCurrent(),
$this->security->getUser(), $this->security->getUser(),
$this->visitorResolver->getCurrent()
$this->visitorResolver->getCurrent(),
true
); );
} }



Loading…
Cancel
Save