<?php
namespace App\Repository;
use App\Entity\BlogPost;
use App\Form\Model\FilterBlogPostModel;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method BlogPost|null find($id, $lockMode = null, $lockVersion = null)
* @method BlogPost|null findOneBy(array $criteria, array $orderBy = null)
* @method BlogPost[] findAll()
* @method BlogPost[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class BlogPostRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, BlogPost::class);
}
/**
* Rechercher les articles par filtre (keyword and title)
*
* @param FilterBlogPostModel $filterBlogPostModel
* @return int|mixed|string
*/
public function getFilteredBlogPost(FilterBlogPostModel $filterBlogPostModel) {
$queryBuilder = $this->createQueryBuilder('p');
if($filterBlogPostModel->getKeyword() !== null) {
$queryBuilder
->where('p.title LIKE :keyword')
->orWhere('p.keyword LIKE :keyword')
->setParameter('keyword', '%'.$filterBlogPostModel->getKeyword().'%')
;
}
$queryBuilder->orderBy('p.createdAt', 'DESC');
return $queryBuilder->getQuery()->getResult();
}
/**
* Get Lasted blogposts
*
* @param int $number
* @return int|mixed|string
*/
public function getLastedArticle(int $number = 3) {
return $this->createQueryBuilder('bp')
->addOrderBy('bp.createdAt', 'DESC')
->setMaxResults($number)
->getQuery()
->getResult();
}
/**
* Find Random Pillar Pages
*
* @param $count
* @return float|int|mixed|string
*/
public function findRandomPillarPages($count, $excludeId = null)
{
$qb = $this->createQueryBuilder('p')
->where('p.isPillarPage = true')
->orderBy('RAND()')
->setMaxResults($count);
if ($excludeId !== null) {
$qb->andWhere('p.id != :excludeId')
->setParameter('excludeId', $excludeId);
}
return $qb->getQuery()
->getResult();
}
/**
* Find only none pillar page on specific category
*
* @param $category
* @param $limit
* @return float|int|mixed|string|null
* @throws NonUniqueResultException
*/
public function findRandomNotPillarPageByCategory($category, $limit = 1)
{
return $this->createQueryBuilder('b')
->where('b.blogCategory = :category')
->andWhere('b.isPillarPage = false')
->setParameter('category', $category)
->orderBy('RAND()') // Mélange aléatoire des résultats
->setMaxResults($limit)
->getQuery()
->getOneOrNullResult();
}
/**
* Retrieve only categories that do not have any pillar blog posts within
*
* @return float|int|mixed|string
*/
public function findCategoriesWithNoPillarArticles()
{
return $this->createQueryBuilder('b')
->select('c.id')
->distinct()
->join('b.blogCategory', 'c')
->where('b.isPillarPage = false')
->getQuery()
->getResult();
}
}