<?php
namespace App\Repository\Posts;
use App\Entity\Posts\Posts;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\NonUniqueResultException;
/**
* @method Posts|null find($id, $lockMode = null, $lockVersion = null)
* @method Posts|null findOneBy(array $criteria, array $orderBy = null)
* @method Posts[] findAll()
* @method Posts[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class PostsRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Posts::class);
}
/**
* @param Posts $post
* @param $categorie
* @param $limit
* @return int|mixed|string
*/
public function getPostRandom(Posts $post,$categorie, $limit)
{
return $this->createQueryBuilder('p')
->select('p', 'm', 'u', 'c')
->leftJoin('p.mediaposts', 'm')
->leftJoin('p.categories', 'c')
->leftJoin('p.user', 'u')
->andWhere('p.id != :post')
->andWhere('c.id = :categorie')
->setParameters(['post' => $post, 'categorie' => $categorie])
->orderBy('RAND()')
->setMaxResults($limit)
->getQuery()
->getResult()
;
}
/**
* @return int|mixed|string|null
* @throws NonUniqueResultException
*/
public function featured()
{
return $this->createQueryBuilder('p')
->andWhere('p.online = 1')
->andWhere('p.featured = 1')
->orderBy('p.updatedAt', 'DESC')
->setMaxResults(1)
->getQuery()
->getOneOrNullResult()
;
}
}