← Back
Editing: ItemRepository.php
<?php namespace App\Repository; use App\Entity\Adresse; use App\Entity\Agency; use App\Entity\Item; use App\Entity\Solution; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\NonUniqueResultException; use Doctrine\ORM\NoResultException; use Doctrine\ORM\Query\Expr; use Doctrine\Persistence\ManagerRegistry; use Symfony\Component\Security\Core\User\UserInterface; /** * @method Item|null find($id, $lockMode = null, $lockVersion = null) * @method Item|null findOneBy(array $criteria, array $orderBy = null) * @method Item[] findAll() * @method Item[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class ItemRepository extends ServiceEntityRepository { use OptionsTrait; public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Item::class); } public function save(Item $entity, bool $flush = false): void { $this->getEntityManager()->persist($entity); if ($flush) { $this->getEntityManager()->flush(); } } public function remove(Item $entity, bool $flush = false): void { $this->getEntityManager()->remove($entity); if ($flush) { $this->getEntityManager()->flush(); } } /** * @return Item[] */ public function findBySolution(Solution $solution): array { return $this->createQueryBuilder('i') ->leftJoin('i.zone', 'z') ->leftJoin('z.root', 'zr') ->leftJoin('zr.thematic', 't') ->where('t.solution = :solution') ->setParameter('solution', $solution) ->getQuery() ->getResult(); } /** * @return Item[] */ public function findByAgencyAddresses(Agency $agency): array { $qb = $this->createQueryBuilder('i'); $qb->leftJoin('i.adresses', 'a') ->where('a.agency = :agency') ->setParameter('agency', $agency) ->groupBy('i'); return $qb->getQuery()->getResult(); } /** * @return array{item: Item, zone: string} * * @throws NoResultException * @throws NonUniqueResultException */ public function getItemByIdWithZone($itemId): array { return $this->createQueryBuilder('i') ->select('i as item') ->addSelect('z.nom as zone') ->leftJoin('i.zone', 'z') ->where('i.id = :itemId') ->setParameter('itemId', $itemId) ->getQuery()->getSingleResult(); } /** * @return Item[] */ public function getAllWithZone(?UserInterface $manager): array { $qb = $this->createQueryBuilder('i') ->select('i.nom as item') ->addSelect('i.id as itemId') ->addSelect('z.nom as zone') ->addSelect('i.notationType as notationType') ->innerJoin('i.zone', 'z') ->groupBy('i'); if ($manager) { $qb->leftJoin('i.adresses', 'ad') ->andWhere('ad.manager = :manager') ->setParameter('manager', $manager); } return $qb ->getQuery() ->getResult(); } public function fetchItemsByAddress(Adresse $address): array { $qb = $this->createQueryBuilder('i') ->select('AVG(n.note) as avgItemNote') ->addSelect('i.id as customId') ->addSelect('i.nom as name') ->addSelect('i as item') ->leftJoin('i.adresses', 'a'); $qb = $this->leftJoinControlsWithOptions($qb, 'a.controles'); $qb ->leftJoin('c.notes', 'n', Expr\Join::WITH, 'n.item = i') ->where('a = :address') ->andWhere('c.valide = :isValid') ->setParameter('address', $address) ->setParameter('isValid', true); $qb = $this->addOptions($qb); $qb->groupBy('i'); return $qb ->getQuery() ->getResult(); } public function fetchItemsByAgencies(array $agenciesId): array { $qb = $this->createQueryBuilder('i'); $qb ->select('AVG(n.note) as avgItemNote') ->addSelect('i.id as customId') ->addSelect('i.nom as name') ->addSelect('i as item') ->leftJoin('i.adresses', 'a'); $qb = $this->leftJoinControlsWithOptions($qb, 'a.controles'); $qb ->leftJoin('c.notes', 'n', Expr\Join::WITH, 'n.item = i') ->where('a.agency IN ('.implode(',', $agenciesId).')') ->andWhere('c.valide = :isValid') ->setParameter('isValid', true); $qb = $this->addOptions($qb); $qb->groupBy('i'); return $qb ->getQuery() ->getResult(); } /** * @return Item[] */ public function fetchInconsistencySolution(Adresse $adresse): array { $qb = $this->createQueryBuilder('i') ->leftJoin('i.zone', 'z') ->leftJoin('z.root', 'zr') ->leftJoin('zr.thematic', 't') ->leftJoin('i.adresses', 'a') ->where('t.solution != :addressSolution') ->andWhere('a = :address') ->setParameter('addressSolution', $adresse->getSolution()) ->setParameter('address', $adresse); return $qb->getQuery()->getResult(); } /** * @return int|mixed|string */ public function getLessRatedItemsByAgencies(array $agenciesId, ?UserInterface $user = null, array $options = []) { $qb = $this->createQueryBuilder('i') ->select('AVG(n.note) as avgItemNote, i.nom as name, z.nom, i.notationType') ->leftJoin('i.adresses', 'a') ->leftJoin('i.zone', 'z'); $qb = $this->leftJoinControlsWithOptions($qb, 'a.controles'); $qb ->leftJoin('c.notes', 'n', Expr\Join::WITH, 'n.item = i'); if (!empty($agenciesId)) { $qb->where('a.agency IN ('.implode(',', $agenciesId).')'); } if ($user && in_array('ROLE_MANAGER', $user->getRoles())) { $qb ->where('a.manager = :manager') ->setParameter('manager', $user); } if ($user && in_array('ROLE_AGENT', $user->getRoles())) { $qb ->where('c.agent = :agent') ->setParameter('agent', $user); } $qb ->andWhere('n.note > 0') ->andWhere('c.valide = :isValid') ->setParameter('isValid', true); $qb = $this->addOptions($qb, false, $options); $qb->orderBy('avgItemNote', 'ASC')->groupBy('i'); $qb->setMaxResults(5); return $qb ->getQuery() ->getResult(); } public function findDefaultByAgency(Agency $agency, Solution $solution): array { return $this->createQueryBuilder('i') ->leftJoin('i.agencies', 'a') ->leftJoin('i.zone', 'z') ->leftJoin('z.root', 'zr') ->leftJoin('zr.thematic', 't') ->where('t.solution = :solution') ->andWhere('a = :agency') ->setParameters(['solution' => $solution, 'agency' => $agency]) ->getQuery() ->getResult(); } }
Save File
Cancel