src/Repository/ProductRepository.php line 135

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Product;
  4. use App\Model\AppConstant;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Persistence\ManagerRegistry;
  7. /**
  8. * @extends ServiceEntityRepository<Product>
  9. *
  10. * @method Product|null find($id, $lockMode = null, $lockVersion = null)
  11. * @method Product|null findOneBy(array $criteria, array $orderBy = null)
  12. * @method Product[] findAll()
  13. * @method Product[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14. */
  15. class ProductRepository extends ServiceEntityRepository
  16. {
  17. public function __construct(ManagerRegistry $registry)
  18. {
  19. parent::__construct($registry, Product::class);
  20. }
  21. public function add(Product $entity, bool $flush = false): void
  22. {
  23. $this->getEntityManager()->persist($entity);
  24. if ($flush) {
  25. $this->getEntityManager()->flush();
  26. }
  27. }
  28. public function remove(Product $entity, bool $flush = false): void
  29. {
  30. $this->getEntityManager()->remove($entity);
  31. if ($flush) {
  32. $this->getEntityManager()->flush();
  33. }
  34. }
  35. public function getSearchQuery($product, $options = null){
  36. $qb = $this->createQueryBuilder('p');
  37. $qb->join('p.produitQteStock', 's');
  38. $qb->addOrderBy('s.isAvailableStock', 'desc');
  39. $parameters = [];
  40. if($product->getName()!=null) {
  41. if ($GLOBALS['app_default_locale'] == 'fr') {
  42. $qb->andWhere('LOWER(p.name) LIKE LOWER(:name)');
  43. $parameters['name'] = '%'.$product->getName(). '%';
  44. } else {
  45. $qb->andWhere('LOWER(p.name_english) LIKE LOWER(:name_english)');
  46. $parameters['name_english'] = '%'.$product->getName(). '%';
  47. }
  48. }
  49. if($product->getDescription()!=null) {
  50. $qb->andWhere('LOWER(p.description) LIKE LOWER(:description)');
  51. $parameters['description'] = '%'.$product->getDescription(). '%';
  52. }
  53. if($product->getCost()!=null) {
  54. $qb->andWhere('p.cost = :cost');
  55. $parameters['cost'] = $product->getCost();
  56. }
  57. if($product->getProductCategory()!=null) {
  58. $qb->andWhere('p.productCategory = :productCategory');
  59. $parameters['productCategory'] = $product->getProductCategory();
  60. }
  61. if($options != null){
  62. if($options['favorite']!=null && $options['favorite'] == 'my_favorite') {
  63. $qb->leftJoin('p.favoriteProducts', 'favoriteProducts');
  64. $qb->andWhere('favoriteProducts.user = :user');
  65. $parameters['user'] = $options['user'];
  66. // $qb->orderBy('p.'.$options['orderBy'], $options['order']);
  67. }
  68. if($options['prixMin']!=null) {
  69. $qb->andWhere('p.cost >= :prixMin');
  70. $parameters['prixMin'] = $options['prixMin'];
  71. }
  72. if($options['prixMax']!=null) {
  73. $qb->andWhere('p.cost <= :prixMax');
  74. $parameters['prixMax'] = $options['prixMax'];
  75. }
  76. if($options['langue']!=null) {
  77. $qb->andWhere('p.langue = :langue');
  78. $parameters['langue'] = $options['langue'];
  79. }else if(isset($options['admin']) && $options['admin'] == false){
  80. $langue = $product::getLanguageIntegerByDefaultLocale();
  81. $qb->andwhere('p.langue = :langue OR p.langue = :all');
  82. $parameters['langue'] = $langue;
  83. $parameters['all'] = $product::$_langue['ALL'];
  84. }
  85. $qb->addOrderBy('s.isAvailableStock', 'desc');
  86. $withOrderOption = ($options['orderBy']!=null && $options['order']!=null);
  87. if($withOrderOption) {
  88. $qb->addOrderBy('p.'.$options['orderBy'], $options['order']);
  89. }
  90. if(!$withOrderOption){
  91. $qb->addOrderBy('p.name', 'asc');
  92. }
  93. }
  94. $qb->andWhere('p.status > :deletedStatus');
  95. $parameters['deletedStatus'] = AppConstant::STATUS_DELETED;
  96. $qb->andWhere('p.productCategory is not null');
  97. $qb->setParameters($parameters);
  98. return $qb->getQuery();
  99. }
  100. public function findAllByDefaultLocale()
  101. {
  102. $qb = $this->createQueryBuilder('p');
  103. $langue = Product::getLanguageIntegerByDefaultLocale();
  104. $qb->andwhere('p.langue = :langue OR p.langue = :all');
  105. $parameters['langue'] = $langue;
  106. $parameters['all'] = Product::$_langue['ALL'];
  107. $qb->andWhere('p.status > :deletedStatus');
  108. $parameters['deletedStatus'] = AppConstant::STATUS_DELETED;
  109. $qb->setParameters($parameters);
  110. return $qb->getQuery()->getResult();
  111. }
  112. public function getFindAllQuery(){
  113. return $this->createQueryBuilder('p')->getQuery();
  114. }
  115. public function getFeaturedProducts($nbr=4): array
  116. {
  117. return $this->createQueryBuilder('p')
  118. ->andWhere('p.featured = 1')
  119. ->andWhere('p.status > :deletedStatus')
  120. ->setParameters([
  121. "deletedStatus" => AppConstant::STATUS_DELETED
  122. ])
  123. ->setMaxResults($nbr)
  124. ->addOrderBy('p.majDate', 'desc')
  125. ->getQuery()
  126. ->getResult()
  127. ;
  128. }
  129. public function getMonthProducts($nbr=1): array
  130. {
  131. return $this->createQueryBuilder('p')
  132. ->andWhere('p.isMonthProduct = 1')
  133. ->andWhere('p.status > :deletedStatus')
  134. ->setParameters([
  135. "deletedStatus" => AppConstant::STATUS_DELETED
  136. ])
  137. ->addOrderBy('p.majDate', 'desc')
  138. ->setMaxResults($nbr)
  139. ->getQuery()
  140. ->getResult()
  141. ;
  142. }
  143. /**
  144. * @return Product[] Returns an array of Product objects
  145. */
  146. public function findPropositionsProduit($produitIdExclude = null, $nbr=4): array
  147. {
  148. $parameters = [];
  149. $qb = $this->createQueryBuilder('p');
  150. if($produitIdExclude != null){
  151. $qb
  152. ->andWhere('p.id != :produitIdExclude');
  153. $parameters ["produitIdExclude"] = $produitIdExclude;
  154. }
  155. $qb->andWhere('p.status > :deletedStatus');
  156. $parameters ["deletedStatus"] = AppConstant::STATUS_DELETED;
  157. return $qb
  158. ->andWhere('p.productCategory is not null')
  159. ->setMaxResults($nbr)
  160. ->setParameters($parameters)
  161. ->getQuery()
  162. ->getResult()
  163. ;
  164. }
  165. // /**
  166. // * @return Product[] Returns an array of Product objects
  167. // */
  168. // public function findByExampleField($value): array
  169. // {
  170. // return $this->createQueryBuilder('p')
  171. // ->andWhere('p.exampleField = :val')
  172. // ->setParameter('val', $value)
  173. // ->orderBy('p.id', 'ASC')
  174. // ->setMaxResults(10)
  175. // ->getQuery()
  176. // ->getResult()
  177. // ;
  178. // }
  179. // public function findOneBySomeField($value): ?Product
  180. // {
  181. // return $this->createQueryBuilder('p')
  182. // ->andWhere('p.exampleField = :val')
  183. // ->setParameter('val', $value)
  184. // ->getQuery()
  185. // ->getOneOrNullResult()
  186. // ;
  187. // }
  188. }