<?php
namespace App\Repository;
use App\Entity\Block;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* BlockRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class BlockRepository extends ServiceEntityRepository {
public function __construct(ManagerRegistry $registry) {
parent::__construct($registry, Block::class);
}
/**
* Stupid wrap to have autocompletion
*/
public function findOneByPath(string $path): ?Block {
return $this->findOneBy(['path' => $path]);
}
/**
* Find one path
* @param string $path path from request : eg'/compagnia-di-autobus/buscenter'
* @param string $host current request host without local prefix 'www.comparabus.it'
*/
public function findOneByPathIgnoreEndingSlash(string $path, string $host): ?Block{
$path = rtrim($path, '/'); //Symfony route seems to always remove ending slash
return $this->createQueryBuilder('b')
->where('b.path = :path')
->orWhere('b.path = :pathSlash')
->orWhere('b.path = :hostPath')
->orWhere('b.path = :hostPathSlash')
->setParameter('path', $path)
->setParameter('pathSlash', $path.'/')
->setParameter('hostPath', $host.$path)
->setParameter('hostPathSlash', $path.'/')
->getQuery()
->getOneOrNullResult();
}
public function findOnebyPathNotNull(string $path): Block {
$block = $this->findOneBy(['path' => $path]);
if (!$block) {
$block = new Block();
$block->setPath($path);
}
return $block;
}
/**
* Get all paths for the different language (remove the language directory)
*/
public function findAllByPath(string $path) {
return $this->createQueryBuilder('b')
->where('b.path LIKE :path')
->setParameter('path', '%'.$path)
->getQuery()
->getResult();
}
/**
* @return Block[]
*/
public function findAllOrderedByPath(): array {
return $this->getEntityManager()
->createQuery(
'SELECT p FROM App:Block p ORDER BY SUBSTRING(p.path,4) ASC'
)
->getResult();
}
/**
* Json search, multiple sql solutions :
* - SELECT id, meta FROM block meta LIKE '%"sitemap":true%';
* - select * from block where meta->"$.sitemap" = true
* - select * from block where JSON_EXTRACT(meta, "$.sitemap") = true
* @return Block[]
*/
public function findAllByMetaSitemapIsTrue() {
return $this->createQueryBuilder('b')
->where('b.meta LIKE :query')
->setParameter('query', '%"sitemap":true%')
->getQuery()
->getResult();
}
}