/
vladislavts
/
gcppmsp
Обзор
Документация
Войти
/
vladislavts
/
gcppmsp
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
prod
src/Repository/CardRepository.php
127 строк
3 KB
vladislav_ts@list.ru
Создана страница со списком записей для специалистов с фильтрами
09 сен 2024, 18:00
09 сен 2024, 18:00
046a05d
Код
Авторство
О чём код?
<?php namespace App\Repository; use App\Entity\Card; use App\Entity\User; use App\Entity\Visitor; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\ORM\OptimisticLockException; use Doctrine\ORM\ORMException; use Doctrine\Persistence\ManagerRegistry; /** * @method Card|null find($id, $lockMode = null, $lockVersion = null) * @method Card|null findOneBy(array $criteria, array $orderBy = null) * @method Card[] findAll() * @method Card[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) */ class CardRepository extends ServiceEntityRepository { public function __construct(ManagerRegistry $registry) { parent::__construct($registry, Card::class); } /** * @throws ORMException * @throws OptimisticLockException */ public function add(Card $entity, bool $flush = true) : void { $this->_em->persist($entity); if ($flush) { $this->_em->flush(); } } /** * @throws ORMException * @throws OptimisticLockException */ public function remove(Card $entity, bool $flush = true) : void { $this_em = $this->getEntityManager(); // dd($this_em->getFilters()->isEnabled("softdeleteable")); $this_em->remove($entity); if ($flush) { $this->_em->flush(); } } public function getTrueCards(int $specialist_id, string $date, int $session_id) { $qb = $this->createQueryBuilder('card') ->andWhere('card.specialist = :specialist') ->andWhere('card.date = :date') ->andWhere('card.session = :session') ->setParameter('specialist', $specialist_id) ->setParameter('date', $date) ->setParameter('session', $session_id) ; $query = $qb->getQuery(); return ($query->execute()); } // public function findAllWithFilters(?User $user, bool $withShowDeleted = false, bool $onlyFuture = true) public function findAllWithFilters(?User $user, $onlyFuture = false, $withShowDeleted = false) : array { // dd($withShowDeleted); if ($withShowDeleted) { $this->getEntityManager()->getFilters()->disable('softdeleteable'); } else { $this->getEntityManager()->getFilters()->enable('softdeleteable'); } $qb = $this->createQueryBuilder('card'); if ($onlyFuture) { $this->addFilterFuture($qb); } if ($user) { $this->addFilterUser($qb, $user); } return $qb ->orderBy('card.date', 'DESC') ->getQuery() ->getResult(); } public function findEmpty(\DateTimeInterface $cur_time) { return $this->createQueryBuilder('card') ->andWhere('card.createdAt < :time') ->andWhere('card.updatedAt = card.createdAt') ->setParameter('time', $cur_time) ->getQuery() ->getResult() ; } ################################ ## Set filters to QueryBuilder ################################ private function addFilterFuture($qb) { return $qb ->andWhere('card.date >= :date') ->setParameter('date', "NOW()") ; } private function addFilterUser($qb, User $user) { return $qb->andWhere('card.specialist = :user')->setParameter('user', $user->getId()); } }