src/Repository/Blog/PostRepository.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Blog;
  3. use App\Entity\Blog\Post;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. /**
  7.  * @method Post|null find($id, $lockMode = null, $lockVersion = null)
  8.  * @method Post|null findOneBy(array $criteria, array $orderBy = null)
  9.  * @method Post[]    findAll()
  10.  * @method Post[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  11.  */
  12. class PostRepository extends ServiceEntityRepository
  13. {
  14.     public function __construct(ManagerRegistry $registry)
  15.     {
  16.         parent::__construct($registryPost::class);
  17.     }
  18.     /**
  19.      * @param string $localeCode: The request's locale
  20.      * @return Post[] Returns an array of Post objects
  21.      */
  22.     public function findPostsQuery($category$keyword)
  23.     {
  24.         $qb $this->createQueryBuilder('p')
  25.             ->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')
  26.             ->innerJoin('p.category''c')
  27.             ->orderBy('p.createdAt''DESC')
  28.         ;
  29.         if ($keyword) {
  30.             $qb->andWhere('(p.title LIKE :keyword OR p.author LIKE :keyword OR p.description LIKE :keyword OR c.name LIKE :keyword)')
  31.                 ->setParameter('keyword''%' $keyword '%');
  32.         } elseif ($category) {
  33.             $qb->andWhere('c.id = :id')->setParameter('id'$category->getId());
  34.         }
  35.         return $qb->getQuery();
  36.     }
  37.     /**
  38.      * @param string $localeCode: The request's locale
  39.      * @param int $limit: How many post to return
  40.      * @return Post[] Returns an array of Post objects
  41.      */
  42.     public function recentPosts($localeCode$limit 10)
  43.     {
  44.         return $this->createQueryBuilder('p')
  45.             ->select('p.id''p.date''p.author''p.link''p.fileDetails.name as pdf''p.image.name as image''pt.title')
  46.             ->innerJoin('p.postTranslations''pt')
  47.             ->innerJoin('pt.locale''l')
  48.             ->andWhere('l.code = :code')
  49.             ->setParameter('code'$localeCode)
  50.             ->orderBy('p.createdAt''DESC')
  51.             ->setMaxResults($limit)
  52.             ->getQuery()
  53.             ->getResult()
  54.         ;
  55.     }
  56.     
  57.     public function findRecent($limit 10)
  58.     {
  59.         return $this->createQueryBuilder('p')
  60.             ->orderBy('p.createdAt''DESC')
  61.             ->setMaxResults($limit)
  62.             ->getQuery()
  63.             ->getResult()
  64.         ;
  65.     }
  66.     
  67. }