src/Repository/Posts/PostsRepository.php line 42

Open in your IDE?
  1. <?php
  2. namespace App\Repository\Posts;
  3. use App\Entity\Posts\Posts;
  4. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Doctrine\ORM\NonUniqueResultException;
  7. /**
  8.  * @method Posts|null find($id, $lockMode = null, $lockVersion = null)
  9.  * @method Posts|null findOneBy(array $criteria, array $orderBy = null)
  10.  * @method Posts[]    findAll()
  11.  * @method Posts[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  12.  */
  13. class PostsRepository extends ServiceEntityRepository
  14. {
  15.     public function __construct(ManagerRegistry $registry)
  16.     {
  17.         parent::__construct($registryPosts::class);
  18.     }
  19.     /**
  20.      * @param Posts $post
  21.      * @param $categorie
  22.      * @param $limit
  23.      * @return int|mixed|string
  24.      */
  25.     public function getPostRandom(Posts $post,$categorie$limit)
  26.     {
  27.         return $this->createQueryBuilder('p')
  28.             ->select('p''m''u''c')
  29.             ->leftJoin('p.mediaposts''m')
  30.             ->leftJoin('p.categories''c')
  31.             ->leftJoin('p.user''u')
  32.             ->andWhere('p.id != :post')
  33.             ->andWhere('c.id = :categorie')
  34.             ->setParameters(['post' => $post'categorie' => $categorie])
  35.             ->orderBy('RAND()')
  36.             ->setMaxResults($limit)
  37.             ->getQuery()
  38.             ->getResult()
  39.             ;
  40.     }
  41.     /**
  42.      * @return int|mixed|string|null
  43.      * @throws NonUniqueResultException
  44.      */
  45.     public function featured()
  46.     {
  47.         return $this->createQueryBuilder('p')
  48.             ->andWhere('p.online = 1')
  49.             ->andWhere('p.featured = 1')
  50.             ->orderBy('p.updatedAt''DESC')
  51.             ->setMaxResults(1)
  52.             ->getQuery()
  53.             ->getOneOrNullResult()
  54.             ;
  55.     }
  56. }