<?php
namespace App\Repository;
use App\Entity\Config;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use App\Utils\CustomPaginator;
/**
* @method Config|null find($id, $lockMode = null, $lockVersion = null)
* @method Config|null findOneBy(array $criteria, array $orderBy = null)
* @method Config[] findAll()
* @method Config[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ConfigRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Config::class);
}
public function getAllConfig($data, $currentPage = 1, $limit = 12, $sort, $order)
{
$parameters = array();
$query = $this->createQueryBuilder('Config');
if(isset($data['value']) && $data['value'] != ""){
$query->andWhere('(
Config.keyName like :where1 or
Config.value like :where1
)');
$parameters[':where1'] = "%".$data['value']."%";
}
if(!empty($parameters)){
$query->setParameters($parameters);
}
$query->addOrderBy($sort, $order);
$query->getQuery();
//dd($query->getQuery()->getResult());
$paginator = new CustomPaginator($query);
$result = $paginator->paginate($currentPage, $limit);
if($result['count'] > 0 && $result['maxPages'] < $result['thisPage']){
$result = $paginator->paginate(1, $limit);
}
return $result;
}
public function getValueOrCreate(string $key, $default = null): ?string
{
$result = $this->findOneBy(['keyName' => $key]);
if($result){
return $result->getValue();
}
if($default === null){
return null;
}
$confg = (new Config)
->setKeyName($key)
->setValue($default)
;
try{
$this->_em->persist($confg);
$this->_em->flush();
return $default;
}catch(\Exception $ex){
return null;
}
}
// /**
// * @return Config[] Returns an array of Config objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->orderBy('c.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?Config
{
return $this->createQueryBuilder('c')
->andWhere('c.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}