em = $entityManager; $this->repository = $entityManager->getRepository(AccompanyingPeriodWork::class); } public function countByAccompanyingPeriod(AccompanyingPeriod $period): int { return $this->repository->countByAccompanyingPeriod($period); } public function countBySocialActionWithDescendants(SocialAction $action): int { $qb = $this->buildQueryBySocialActionWithDescendants($action); $qb->select('COUNT(g)'); return $qb ->getQuery() ->getSingleScalarResult(); } public function countNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): int { return $this->buildQueryNearEndDateByUser($user, $since, $until) ->select('count(w)')->getQuery()->getSingleScalarResult(); } public function createQueryBuilder(string $alias, ?string $indexBy = null): QueryBuilder { return $this->repository->createQueryBuilder($alias, $indexBy); } public function find($id): ?AccompanyingPeriodWork { return $this->repository->find($id); } public function findAll(): array { return $this->repository->findAll(); } public function findBy(array $criteria, ?array $orderBy = null, $limit = null, $offset = null): array { return $this->repository->findBy($criteria, $orderBy, $limit, $offset); } /** * @param mixed|null $orderBy * @param mixed|null $limit * @param mixed|null $offset * * @return AccompanyingPeriodWork[] */ public function findByAccompanyingPeriod(AccompanyingPeriod $period, $orderBy = null, $limit = null, $offset = null): array { return $this->repository->findByAccompanyingPeriod($period, $orderBy, $limit, $offset); } /** * Return a list of accompanying period with a defined order:. * * * first, opened works * * then, closed works * * @return AccompanyingPeriodWork[] * @param mixed $filters */ public function findByAccompanyingPeriodOpenFirst(AccompanyingPeriod $period, $filters, int $limit = 10, int $offset = 0): array { $rsm = new ResultSetMappingBuilder($this->em); $rsm->addRootEntityFromClassMetadata(AccompanyingPeriodWork::class, 'w'); $sql = "SELECT {$rsm} FROM chill_person_accompanying_period_work w WHERE accompanyingPeriod_id = :periodId ORDER BY CASE WHEN enddate IS NULL THEN '-infinity'::timestamp ELSE 'infinity'::timestamp END ASC, startdate DESC, enddate DESC, id DESC LIMIT :limit OFFSET :offset"; $nq = $this->em->createNativeQuery($sql, $rsm) ->setParameter('periodId', $period->getId(), Types::INTEGER) ->setParameter('limit', $limit, Types::INTEGER) ->setParameter('offset', $offset, Types::INTEGER); return $nq->getResult(); } public function findNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until, int $limit = 20, int $offset = 0): array { return $this->buildQueryNearEndDateByUser($user, $since, $until) ->select('w') ->setFirstResult($offset) ->setMaxResults($limit) ->getQuery() ->getResult(); } public function findOneBy(array $criteria): ?AccompanyingPeriodWork { return $this->repository->findOneBy($criteria); } public function getClassName() { return AccompanyingPeriodWork::class; } private function buildQueryBySocialActionWithDescendants(SocialAction $action): QueryBuilder { $actions = $action->getDescendantsWithThis(); $qb = $this->repository->createQueryBuilder('g'); $orx = $qb->expr()->orX(); $i = 0; foreach ($actions as $a) { $orx->add(":action_{$i} MEMBER OF g.socialActions"); $qb->setParameter("action_{$i}", $a); } $qb->where($orx); return $qb; } private function buildQueryNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until): QueryBuilder { $qb = $this->repository->createQueryBuilder('w'); $qb ->join('w.accompanyingPeriod', 'period') ->where( $qb->expr()->andX( $qb->expr()->gte('w.endDate', ':since'), $qb->expr()->lte('w.startDate', ':until'), $qb->expr()->orX( $qb->expr()->eq('period.user', ':user'), $qb->expr()->isMemberOf(':user', 'w.referrers') ) ) ) ->setParameters([ 'user' => $user, 'since' => $since, 'until' => $until, ]); return $qb; } }