src/Repository/BlogPostRepository.php line 79

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\BlogPost;
  4. use App\Form\Model\FilterBlogPostModel;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\ORM\NonUniqueResultException;
  7. use Doctrine\Persistence\ManagerRegistry;
  8. /**
  9.  * @method BlogPost|null find($id, $lockMode = null, $lockVersion = null)
  10.  * @method BlogPost|null findOneBy(array $criteria, array $orderBy = null)
  11.  * @method BlogPost[]    findAll()
  12.  * @method BlogPost[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  13.  */
  14. class BlogPostRepository extends ServiceEntityRepository
  15. {
  16.     public function __construct(ManagerRegistry $registry)
  17.     {
  18.         parent::__construct($registryBlogPost::class);
  19.     }
  20.     /**
  21.      * Rechercher les articles par filtre (keyword and title)
  22.      *
  23.      * @param FilterBlogPostModel $filterBlogPostModel
  24.      * @return int|mixed|string
  25.      */
  26.     public function getFilteredBlogPost(FilterBlogPostModel $filterBlogPostModel) {
  27.         $queryBuilder $this->createQueryBuilder('p');
  28.         if($filterBlogPostModel->getKeyword() !== null) {
  29.             $queryBuilder
  30.                 ->where('p.title LIKE :keyword')
  31.                 ->orWhere('p.keyword LIKE :keyword')
  32.                 ->setParameter('keyword''%'.$filterBlogPostModel->getKeyword().'%')
  33.             ;
  34.         }
  35.         $queryBuilder->orderBy('p.createdAt''DESC');
  36.         return $queryBuilder->getQuery()->getResult();
  37.     }
  38.     /**
  39.      * Get Lasted blogposts
  40.      *
  41.      * @param int $number
  42.      * @return int|mixed|string
  43.      */
  44.     public function getLastedArticle(int $number 3) {
  45.         return $this->createQueryBuilder('bp')
  46.             ->addOrderBy('bp.createdAt''DESC')
  47.             ->setMaxResults($number)
  48.             ->getQuery()
  49.             ->getResult();
  50.     }
  51.     /**
  52.      * Find Random Pillar Pages
  53.      *
  54.      * @param $count
  55.      * @return float|int|mixed|string
  56.      */
  57.     public function findRandomPillarPages($count$excludeId null)
  58.     {
  59.         $qb $this->createQueryBuilder('p')
  60.             ->where('p.isPillarPage = true')
  61.             ->orderBy('RAND()')
  62.             ->setMaxResults($count);
  63.         if ($excludeId !== null) {
  64.             $qb->andWhere('p.id != :excludeId')
  65.                 ->setParameter('excludeId'$excludeId);
  66.         }
  67.         return $qb->getQuery()
  68.             ->getResult();
  69.     }
  70.     /**
  71.      * Find only none pillar page on specific category
  72.      *
  73.      * @param $category
  74.      * @param $limit
  75.      * @return float|int|mixed|string|null
  76.      * @throws NonUniqueResultException
  77.      */
  78.     public function findRandomNotPillarPageByCategory($category$limit 1)
  79.     {
  80.         return $this->createQueryBuilder('b')
  81.             ->where('b.blogCategory = :category')
  82.             ->andWhere('b.isPillarPage = false')
  83.             ->setParameter('category'$category)
  84.             ->orderBy('RAND()'// Mélange aléatoire des résultats
  85.             ->setMaxResults($limit)
  86.             ->getQuery()
  87.             ->getOneOrNullResult();
  88.     }
  89.     /**
  90.      * Retrieve only categories that do not have any pillar blog posts within
  91.      *
  92.      * @return float|int|mixed|string
  93.      */
  94.     public function findCategoriesWithNoPillarArticles()
  95.     {
  96.         return $this->createQueryBuilder('b')
  97.             ->select('c.id')
  98.             ->distinct()
  99.             ->join('b.blogCategory''c')
  100.             ->where('b.isPillarPage = false')
  101.             ->getQuery()
  102.             ->getResult();
  103.     }
  104. }