1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
Blog/src/Repository/PostRepository.php
2021-01-16 18:25:29 +01:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\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);
}
public function getPublished(int $offset = null, int $max = null) {
$q = $this->createQueryBuilder("p")
->andWhere('p.publishedAt <= :now')
->setParameter('now', new \DateTime())
->orderBy('p.publishedAt', 'DESC');
if ($offset) {
$q->setFirstResult($offset);
}
if ($max) {
$q->setMaxResults($max);
}
return $q->getQuery()
->getResult();
}
// /**
// * @return Post[] Returns an array of Post objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Post
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}