*/ class SmartLinkRepository extends CommonRepository { public function getTableAlias(): string { return 'sl'; } /** * Loads the page and the form with the links, so that the list doesn't query them one by one. * * @param array $args */ public function getEntities(array $args = []) { $alias = $this->getTableAlias(); // The hit count of each link as a HIDDEN scalar: not hydrated into the entities, but // available as an order by column ("sl.hitCount") for the Hits column of the list. $hitCount = '(SELECT COUNT(h.id) FROM '.SmartLinkHit::class.' h WHERE h.smartLink = '.$alias.'.id) AS HIDDEN hitCount'; $args['qb'] = $this->createQueryBuilder($alias) ->select($alias, 'pg', 'fm', $hitCount) ->leftJoin($alias.'.page', 'pg') ->leftJoin($alias.'.form', 'fm'); return parent::getEntities($args); } /** * True when another link already uses this slug. A link being edited never collides with itself. */ public function slugExists(string $slug, ?int $ignoreId = null): bool { if ('' === $slug) { return false; } $qb = $this->createQueryBuilder($this->getTableAlias()) ->select('COUNT('.$this->getTableAlias().'.id)') ->where($this->getTableAlias().'.slug = :slug') ->setParameter('slug', $slug); if (null !== $ignoreId) { $qb->andWhere($this->getTableAlias().'.id != :id') ->setParameter('id', $ignoreId); } return (int) $qb->getQuery()->getSingleScalarResult() > 0; } /** * The list's search box matches the slug and the destination URL. * * @return array */ protected function addCatchAllWhereClause($q, $filter): array { return $this->addStandardCatchAllWhereClause($q, $filter, [ $this->getTableAlias().'.slug', $this->getTableAlias().'.destinationUrl', ]); } /** * The Hits column of the list sorts by the number of hits of each link: the virtual * "hitCount" order by maps to the HIDDEN hitCount scalar of the list query. Everything * else falls back to core's column handling. */ protected function buildOrderByClause($q, array $args): void { $alias = $this->getTableAlias(); $orderBy = (string) ($args['orderBy'] ?? ''); $virtual = $alias.'.hitCount' === $orderBy || 'hitCount' === $orderBy; if ($virtual && $q instanceof \Doctrine\ORM\QueryBuilder) { $orderByDir = $this->sanitize((string) ($args['orderByDir'] ?? 'ASC')); $q->addOrderBy('hitCount', $orderByDir); return; } parent::buildOrderByClause($q, $args); } /** * @return array */ protected function getDefaultOrder(): array { return [ [$this->getTableAlias().'.slug', 'ASC'], ]; } }