<?php
namespace App\Repository\Blog;
use App\Entity\Blog\Post;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Post|null find($id, $lockMode = null, $lockVersion = null)
* @method Post|null findOneBy(array $criteria, array $orderBy = null)
* @method Post[] findAll()
* @method Post[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PostRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Post::class);
}
/**
* @param string $localeCode: The request's locale
* @return Post[] Returns an array of Post objects
*/
public function findPostsQuery($category, $keyword)
{
$qb = $this->createQueryBuilder('p')
->select('p.id', 'p.author', 'p.link', 'p.fileDetails.name as pdf', 'p.date', 'p.image.name as image', 'p.title', 'p.description', 'p.createdAt', 'c.name as category', 'c.nameEng as categoryEng')
->innerJoin('p.category', 'c')
->orderBy('p.createdAt', 'DESC')
;
if ($keyword) {
$qb->andWhere('(p.title LIKE :keyword OR p.author LIKE :keyword OR p.description LIKE :keyword OR c.name LIKE :keyword)')
->setParameter('keyword', '%' . $keyword . '%');
} elseif ($category) {
$qb->andWhere('c.id = :id')->setParameter('id', $category->getId());
}
return $qb->getQuery();
}
/**
* @param string $localeCode: The request's locale
* @param int $limit: How many post to return
* @return Post[] Returns an array of Post objects
*/
public function recentPosts($localeCode, $limit = 10)
{
return $this->createQueryBuilder('p')
->select('p.id', 'p.date', 'p.author', 'p.link', 'p.fileDetails.name as pdf', 'p.image.name as image', 'pt.title')
->innerJoin('p.postTranslations', 'pt')
->innerJoin('pt.locale', 'l')
->andWhere('l.code = :code')
->setParameter('code', $localeCode)
->orderBy('p.createdAt', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
public function findRecent($limit = 10)
{
return $this->createQueryBuilder('p')
->orderBy('p.createdAt', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
}