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 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); } 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()->eq('period.user', ':user'), $qb->expr()->gte('w.endDate', ':since'), $qb->expr()->lte('w.startDate', ':until') ) ) ->setParameters([ 'user' => $user, 'since' => $since, 'until' => $until, ]); return $qb; } }