src/Repository/CompanyRepository.php line 113

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Company;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\ORM\Query\Expr\GroupBy;
  6. use Doctrine\ORM\Tools\Pagination\Paginator;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. use Psr\Log\LoggerInterface;
  9. /**
  10.  * @method Company|null find($id, $lockMode = null, $lockVersion = null)
  11.  * @method Company|null findOneBy(array $criteria, array $orderBy = null)
  12.  * @method Company[]    findAll()
  13.  * @method Company[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  14.  */
  15. class CompanyRepository extends ServiceEntityRepository
  16. {
  17.   private ?LoggerInterface $logger;
  18.   public function __construct(
  19.     ManagerRegistry $registry,
  20.     LoggerInterface $logger
  21.    ) {
  22.     parent::__construct($registryCompany::class);
  23.     $this->logger $logger;
  24.   }
  25.   public function search_all_companies()
  26.   {
  27.     $param 0;
  28.     $qb $this->createQueryBuilder('c');
  29.     // Récupération de toutes les entreprises
  30.     $qb->where('c.id >= :val')
  31.       ->setParameter('val''1');
  32.     $query $qb->getQuery();
  33.     return $query->execute();
  34.   }
  35.   public function getCompanyWithCategoriesAndProducts($id)
  36.   {
  37.     return $this->createQueryBuilder('p')
  38.       ->select('p')
  39.       ->where('p.id = :id')
  40.       ->leftJoin('p.categories''c')
  41.       ->addSelect('partial c.{id, name}')
  42.       ->leftJoin('p.products''d')
  43.       ->addSelect('d')
  44.       ->setParameter('id'$id)
  45.       ->getQuery()
  46.       ->getArrayResult();
  47.   }
  48.   //this is new one , the old functon is just below this one
  49.   public function getCompaniesByCatAndSubcat($catId$subcatId)
  50.   {
  51.     return $this->createQueryBuilder('q')
  52.       ->select('q,p,c,s')
  53.       ->innerjoin('q.products''p')
  54.       ->innerjoin('q.categories''c')
  55.       ->innerjoin('p.subcategories''s')
  56.       ->andwhere('s.id = :subcatId')
  57.       ->setParameter('subcatId'$subcatId)
  58.       ->andwhere('c.id = :catId')
  59.       ->setParameter('catId'$catId)
  60.       ->andwhere('q.activated = 1')
  61.       ->getQuery();
  62.     //->getArrayResult();
  63.   }
  64.   //this is new one , the old functon is just below this one
  65.   public function getCompaniesByCat($catId)
  66.   {
  67.     return $this->createQueryBuilder('q')
  68.       ->select('q,p,c,s,sc')
  69.       ->innerjoin('q.products''p')
  70.       ->innerjoin('q.categories''c')
  71.       ->innerjoin('p.subcategories''s')
  72.       ->innerjoin('s.categories''sc')
  73.       ->andwhere('c.id = :catId')
  74.       ->setParameter('catId'$catId)
  75.       ->andwhere('q.activated = 1')
  76.       ->getQuery()
  77.       ->getArrayResult();
  78.   }
  79.   //this is new one , the old functon is just below this one
  80.   public function getCompaniesDetailsById($id)
  81.   {
  82.     return $this->createQueryBuilder('q')
  83.       ->select('q,p,c,s')
  84.       ->andwhere('q.id = :id')
  85.       ->setParameter('id'$id)
  86.       ->innerjoin('q.products''p')
  87.       ->innerjoin('q.categories''c')
  88.       ->innerjoin('p.subcategories''s')
  89.       ->andwhere('q.activated = 1')
  90.       ->getQuery();
  91.     //->getArrayResult();
  92.   }
  93.   public function findLatestCompanies($date)
  94.   {
  95.     $companies $this->createQueryBuilder('e')
  96.       ->select('e.id, e.name, e.image, e.startingdate, e.longtitude, e.latitude, u.inscriptiondate')
  97.       ->innerJoin('App\Entity\User''u''WITH''u.id = e.id')
  98.       ->andWhere('u.inscriptiondate >= :date')
  99.       ->andWhere('u.role = :role')
  100.       ->groupBy('e.id')
  101.       ->orderBy('u.inscriptiondate''DESC')
  102.       ->setParameter('date'$date)
  103.       ->setParameter('role''ROLE_COMPANY')
  104.       ->getQuery()
  105.       ->getArrayResult();
  106.     foreach ($companies as &$company) {
  107.       $categories $this->createQueryBuilder('e')
  108.         ->select('c.id as category_id, c.name as category_name')
  109.         ->leftJoin('e.categories''c')
  110.         ->where('e.id = :companyId')
  111.         ->setParameter('companyId'$company['id'])
  112.         ->getQuery()
  113.         ->getArrayResult();
  114.       $company['categories'] = $categories;
  115.     }
  116.     return $companies;
  117.   }
  118.   public function findTheTwelveLastCompanies()
  119.   {
  120.     return $this->createQueryBuilder('e')
  121.       ->select('e.id, e.name, e.description, e.latitude, e.longtitude, u.inscriptiondate')
  122.       ->innerJoin('App\Entity\User''u''WITH''u.id = e.id')
  123.       ->andWhere('u.role = :role')
  124.       ->orderBy('u.inscriptiondate''DESC')
  125.       ->setParameter('role''ROLE_COMPANY')
  126.       ->setMaxResults(12)
  127.       ->getQuery()
  128.       ->getArrayResult();
  129.   }
  130.   // public function getCompaniesByCatAndSubcat($catId, $subcatId)
  131.   // {
  132.   //   return $this->createQueryBuilder('c')
  133.   //     ->where('c.activated = 1')
  134.   //     ->join('c.categories', 'ca')
  135.   //     ->andWhere('ca.id = :catId')
  136.   //     ->join('ca.subcategories', 's')
  137.   //     ->andWhere('s.id = :subcatId ')
  138.   //     ->join('s.products', 'p')
  139.   //     ->setParameters(['subcatId' => $subcatId, 'catId' => $catId])
  140.   //     ->orderBy('c.niveau', 'DESC')
  141.   //     ->getQuery();
  142.   //   //->getResult();
  143.   // }
  144.   // /**
  145.   //  * @return Company[] Returns an array of Company objects
  146.   //  */
  147.   /*
  148.     public function findByExampleField($value)
  149.     {
  150.         return $this->createQueryBuilder('c')
  151.             ->andWhere('c.exampleField = :val')
  152.             ->setParameter('val', $value)
  153.             ->orderBy('c.id', 'ASC')
  154.             ->setMaxResults(10)
  155.             ->getQuery()
  156.             ->getResult()
  157.         ;
  158.     }
  159.     */
  160.   public function findCompaniesByDate($date)
  161.   {
  162.     return $this->createQueryBuilder('e')
  163.       ->select('e.id, e.name, e.image, e.latitude, e.longtitude, u.inscriptiondate')
  164.       ->innerJoin('App\Entity\User''u''WITH''u.id = e.id')
  165.       ->andWhere('u.inscriptiondate >= :date')
  166.       ->andWhere('u.role = :role')
  167.       ->orderBy('u.inscriptiondate''DESC')
  168.       ->setParameter('date'$date)
  169.       ->setParameter('role''ROLE_COMPANY')
  170.       ->getQuery()
  171.       ->getArrayResult();
  172.   }
  173.   public function findCompaniesToday()
  174.   {
  175.     return $this->createQueryBuilder('e')
  176.       ->innerJoin('App\Entity\User''u''WITH''u.id = e.id')
  177.       ->andWhere('CURRENT_DATE() = u.inscriptiondate')
  178.       ->andWhere('u.role = :role')
  179.       ->orderBy('u.inscriptiondate''DESC')
  180.       ->setParameter('role''ROLE_COMPANY')
  181.       ->setMaxResults(3)
  182.       ->getQuery()
  183.       ->getResult();
  184.   }
  185.   public function findCompaniesThisMonth()
  186.   {
  187.     $year date("y");
  188.     $month date("m");
  189.     $fromTime = new \DateTime($year '-' $month '-00');
  190.     return $this->createQueryBuilder('e')
  191.       ->innerJoin('App\Entity\User''u''WITH''u.id = e.id')
  192.       ->andWhere('u.inscriptiondate > :val')
  193.       ->andWhere('u.role = :role')
  194.       ->setParameter('val'$fromTime)
  195.       ->setParameter('role''ROLE_COMPANY')
  196.       ->orderBy('u.inscriptiondate''DESC')
  197.       ->getQuery()
  198.       ->getResult();
  199.   }
  200.   public function findCompaniesSince($old_date)
  201.   {
  202.     $year $old_date->format("y");
  203.     $month $old_date->format("m");
  204.     $fromTime = new \DateTime($year '-' $month '-00');
  205.     return $this->createQueryBuilder('c')
  206.       ->innerJoin('App\Entity\User''u''WITH''u.id = c.id')
  207.       ->andWhere('u.inscriptiondate >= :date')
  208.       ->andWhere('u.role = :role')
  209.       ->setParameter('date'$fromTime)
  210.       ->setParameter('role''ROLE_COMPANY')
  211.       ->orderBy('u.inscriptiondate''DESC')
  212.       ->getQuery()
  213.       ->getResult();
  214.   }
  215.   public function findCompaniesThisWeek()
  216.   {
  217.     $fromTime = new \DateTime();
  218.     $q $fromTime->format('y-m-d');
  219.     $date strtotime($q);
  220.     $n date('W'$date);
  221.     return $this->createQueryBuilder('e')
  222.       ->innerJoin('App\Entity\User''u''WITH''u.id = e.id')
  223.       ->andWhere('WEEK(u.inscriptiondate) = :val')
  224.       ->andWhere('u.role = :role')
  225.       ->setParameter('val'$n)
  226.       ->setParameter('role''ROLE_COMPANY')
  227.       ->orderBy('u.inscriptiondate''DESC')
  228.       ->getQuery()
  229.       ->getResult();
  230.   }
  231. }