src/Repository/CubeRepository.php line 78

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Cube;
  4. use App\Entity\Prod;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. use Symfony\Contracts\Cache\ItemInterface;
  9. use Symfony\Contracts\Cache\TagAwareCacheInterface;
  10. /**
  11.  * @extends ServiceEntityRepository<Cube>
  12.  *
  13.  * @method Cube|null find($id, $lockMode = null, $lockVersion = null)
  14.  * @method Cube|null findOneBy(array $criteria, array $orderBy = null)
  15.  * @method Cube[]    findAll()
  16.  * @method Cube[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  17.  */
  18. class CubeRepository extends ServiceEntityRepository
  19. {
  20.     public function __construct(
  21.         ManagerRegistry $registry,
  22.         private CharvalRepository $charvalRepository,
  23.         private ProdRepository $prodRepository,
  24.         private TagAwareCacheInterface $appCache,
  25.         private RequestStack $requestStack,
  26.     )
  27.     {
  28.         parent::__construct($registryCube::class);
  29.     }
  30.     public function save(Cube $entitybool $flush false): void
  31.     {
  32.         $this->getEntityManager()->persist($entity);
  33.         if ($flush) {
  34.             $this->getEntityManager()->flush();
  35.         }
  36.     }
  37.     public function remove(Cube $entitybool $flush false): void
  38.     {
  39.         $this->getEntityManager()->remove($entity);
  40.         if ($flush) {
  41.             $this->getEntityManager()->flush();
  42.         }
  43.     }
  44.     /**
  45.      * Универсальный фильтр по кубу
  46.      * пример:
  47.      * $filters = ['color' => '111', 'size' => '222', 'pack' => 1]
  48.      */
  49.     public function filter(string $model, array $filters): array
  50.     {
  51.         $qb $this->em->createQueryBuilder()
  52.             ->select('t')
  53.             ->from(Cube::class, 't')
  54.             ->where('t.model = :model')
  55.             ->setParameter('model'$model);
  56.         foreach ($filters as $field => $value) {
  57.             switch ($field) {
  58.                 case 'size':  $qb->andWhere('s.name = :size')->setParameter('size'$value); break;
  59.                 case 'color'$qb->andWhere('c.name = :color')->setParameter('color'$value); break;
  60.                 case 'pack':  $qb->andWhere('pack.name = :pack')->setParameter('pack'$value); break;
  61.             }
  62.         }
  63.         return $qb->getQuery()->getResult();
  64.     }
  65.     public function getColors(string $modelint $sizestring $inpackint $prod_id_except 0): array
  66.     {
  67.         $colors $this->appCache->get('cube_'.$model."_s".$size."_i".$inpack."_".$this->requestStack->getCurrentRequest()->getLocale(), function (ItemInterface $item) use ($model$size$inpack$prod_id_except): array {
  68.             if ($prod_id_except) {
  69.                 $sql "SELECT prod, `color` FROM cube WHERE `model` = '".$model."' AND `size` = $size AND inpack = '$inpack' AND prod != $prod_id_except";
  70.             } else {
  71.                 $sql "SELECT prod, `color` FROM cube WHERE `model` = '".$model."' AND `size` = $size AND inpack = '$inpack' GROUP BY color order by size";
  72.             }
  73.             
  74.             $results $this->getEntityManager()->getConnection()->prepare($sql)->executeQuery()->fetchAllAssociative();
  75.             $colors = [];
  76.             foreach ($results as $k => $r) {
  77.                 if (!isset($colors[$k])) {
  78.                     $colors[$k] = [];
  79.                 }
  80.                 $colors[$k]['prod'] = $this->prodRepository->find($r['prod']);
  81.                 $colors[$k]['color'] = $this->charvalRepository->find($r['color']);
  82.             }
  83.             return $colors;
  84.         });
  85.         return $colors;
  86.     }
  87.     public function getSizes(string $modelint $colorstring $inpackint $prod_id_except 0): array
  88.     {
  89.         $sizes $this->appCache->get('cube_'.$model."_c".$color."_i".$inpack."_".$this->requestStack->getCurrentRequest()->getLocale(), function (ItemInterface $item) use ($model$color$inpack$prod_id_except): array {
  90.             if ($prod_id_except) {
  91.                 $sql "SELECT prod, `size` FROM cube WHERE `model` = '".$model."' AND `color` = $color AND inpack = '$inpack' prod != $prod_id_except";
  92.             } else {
  93.                 $sql "SELECT prod, `size` FROM cube WHERE `model` = '".$model."' AND `color` = $color AND inpack = '$inpack' GROUP BY size order by color";
  94.             }
  95.             $results $this->getEntityManager()->getConnection()->prepare($sql)->executeQuery()->fetchAllAssociative();
  96.             $sizes = [];
  97.             foreach ($results as $k => $r) {
  98.                 if (!isset($sizes[$k])) {
  99.                     $sizes[$k] = [];
  100.                 }
  101.                 $sizes[$k]['prod'] = $this->prodRepository->find($r['prod']);
  102.                 $sizes[$k]['size'] = $this->charvalRepository->find($r['size']);
  103.             }
  104.             
  105.             return $sizes;
  106.         });
  107.         return $sizes;
  108.     }
  109.     public function getPacks(string $modelint $colorint $sizeint $prod_id_except 0): array
  110.     {
  111.         $packs $this->appCache->get('cube_'.$model."_c".$color."_s".$size."_".$this->requestStack->getCurrentRequest()->getLocale(), function (ItemInterface $item) use ($model$color$size$prod_id_except): array {
  112.             if ($prod_id_except) {
  113.                 $sql "SELECT prod, `inpack` FROM cube WHERE `model` = '".$model."' AND `color` = $color AND `size` = $size AND prod != $prod_id_except";
  114.             } else {
  115.                 $sql "SELECT prod, `inpack` FROM cube WHERE `model` = '".$model."' AND `color` = $color AND `size` = $size GROUP BY inpack order by prod";
  116.             }
  117.             $results $this->getEntityManager()->getConnection()->prepare($sql)->executeQuery()->fetchAllAssociative();
  118.             $packs = [];
  119.             foreach ($results as $k => $r) {
  120.                 if (!isset($packs[$k])) {
  121.                     $packs[$k] = [];
  122.                 }
  123.                 $packs[$k]['prod'] = $this->prodRepository->find($r['prod']);
  124.                 $packs[$k]['inpack'] = $packs[$k]['prod']->getInpack();
  125.             }
  126.             
  127.             return $packs;
  128.         });
  129.         return $packs;
  130.     }
  131.     public function getProd(string $modelint $sizeint $colorstring $inpack): ?Prod
  132.     {
  133.         $sql "SELECT `prod` FROM cube WHERE `model` = '".$model."' AND `color` = $color AND `size` = $size AND `inpack` = '$inpack'";
  134.         $results $this->getEntityManager()->getConnection()->prepare($sql)->executeQuery()->fetchAssociative();
  135.         if (isset($results['prod'])) {
  136.             return $this->prodRepository->find($results['prod']);
  137.         } else {
  138.             return null;
  139.         }
  140.     }
  141. }