Spookie/src/Repository/QuotesRepository.php

95 lines
2.4 KiB
PHP

<?php
namespace App\Repository;
use App\Entity\Quotes;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Quotes|null find($id, $lockMode = null, $lockVersion = null)
* @method Quotes|null findOneBy(array $criteria, array $orderBy = null)
* @method Quotes[] findAll()
* @method Quotes[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class QuotesRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct(registry: $registry, entityClass: Quotes::class);
}
/**
*/
public function add(Quotes $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
*/
public function remove(Quotes $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @return null|float|int|mixed|string
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function findOneRandom(): mixed
{
$idLimits = $this->createQueryBuilder(alias: 'q')
->select('MIN(q.id)', 'MAX(q.id)')
->getQuery()
->getOneOrNullResult();
$randomPossibleId = rand(min: $idLimits[1], max: $idLimits[2]);
return $this->createQueryBuilder(alias: 'q')
->where(predicates: 'q.id >= :random_id')
->setParameter(key: 'random_id', value: $randomPossibleId)
->setMaxResults(maxResults: 1)
->getQuery()
->getOneOrNullResult();
}
// /**
// * @return Quotes[] Returns an array of Quotes objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('q')
->andWhere('q.exampleField = :val')
->setParameter('val', $value)
->orderBy('q.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Quotes
{
return $this->createQueryBuilder('q')
->andWhere('q.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}