src/Repository/BlockRepository.php line 46

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Block;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * BlockRepository
  8.  *
  9.  * This class was generated by the Doctrine ORM. Add your own custom
  10.  * repository methods below.
  11.  */
  12. class BlockRepository extends ServiceEntityRepository {
  13.     public function __construct(ManagerRegistry $registry) {
  14.         parent::__construct($registryBlock::class);
  15.     }
  16.     /**
  17.      * Stupid wrap to have autocompletion
  18.      */
  19.     public function findOneByPath(string $path): ?Block {
  20.         return $this->findOneBy(['path' => $path]);
  21.     }
  22.     /**
  23.      * Find one path
  24.      * @param string $path path from request : eg'/compagnia-di-autobus/buscenter'
  25.      * @param string $host current request host without local prefix 'www.comparabus.it'
  26.      */
  27.     public function findOneByPathIgnoreEndingSlash(string $pathstring $host): ?Block{
  28.         $path rtrim($path'/'); //Symfony route seems to always remove ending slash
  29.         return $this->createQueryBuilder('b')
  30.             ->where('b.path = :path')
  31.             ->orWhere('b.path = :pathSlash')
  32.             ->orWhere('b.path = :hostPath')
  33.             ->orWhere('b.path = :hostPathSlash')
  34.             ->setParameter('path'$path)
  35.             ->setParameter('pathSlash'$path.'/')
  36.             ->setParameter('hostPath'$host.$path)
  37.             ->setParameter('hostPathSlash'$path.'/')
  38.             ->getQuery()
  39.             ->getOneOrNullResult();
  40.     }
  41.     public function findOnebyPathNotNull(string $path): Block {
  42.         $block $this->findOneBy(['path' => $path]);
  43.         if (!$block) {
  44.             $block = new Block();
  45.             $block->setPath($path);
  46.         }
  47.         return $block;
  48.     }
  49.     /**
  50.      * Get all paths for the different language (remove the language directory)
  51.      */
  52.     public function findAllByPath(string $path) {
  53.         return $this->createQueryBuilder('b')
  54.             ->where('b.path LIKE :path')
  55.             ->setParameter('path''%'.$path)
  56.             ->getQuery()
  57.             ->getResult();
  58.     }
  59.     /**
  60.      * @return Block[]
  61.      */
  62.     public function findAllOrderedByPath(): array {
  63.         return $this->getEntityManager()
  64.             ->createQuery(
  65.                 'SELECT p FROM App:Block p ORDER BY SUBSTRING(p.path,4) ASC'
  66.             )
  67.             ->getResult();
  68.     }
  69.     /**
  70.      * Json search, multiple sql solutions :
  71.      * - SELECT id, meta FROM block meta LIKE '%"sitemap":true%';
  72.      * - select * from block where meta->"$.sitemap" = true
  73.      * - select * from block where JSON_EXTRACT(meta, "$.sitemap") = true
  74.      * @return Block[]
  75.      */
  76.     public function findAllByMetaSitemapIsTrue() {
  77.         return $this->createQueryBuilder('b')
  78.             ->where('b.meta LIKE :query')
  79.             ->setParameter('query''%"sitemap":true%')
  80.             ->getQuery()
  81.             ->getResult();
  82.     }
  83. }