<?php
namespace Kobizo\Bundle\CoreBundle\Repository;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Doctrine\ORM\QueryBuilder;
use Kobizo\Bundle\ECommerceBundle\Entity\Store;
use Kobizo\Component\Entity\Option;
/**
* @method Option|null find($id, $lockMode = null, $lockVersion = null)
* @method Option|null findOneBy(array $criteria, array $orderBy = null)
* @method Option[] findAll()
* @method Option[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class OptionRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Option::class);
}
public function getSearchQueryBuilder(array $filters = []): QueryBuilder
{
$qb = $this->createQueryBuilder('option');
if (isset($filters['term'])) {
$qb->andWhere('option.key LIKE :term OR option.label LIKE :term')
->setParameter('term', '%'.$filters['term'].'%');
}
return $qb;
}
public function getOptionByKeyAndStoreCode(string $key, ?Store $store = null): ?Option
{
$qb = $this->createQueryBuilder('option')
->where('option.key = :key')
->setParameter('key', $key);
if ($store) {
$qb->join('option.store', 'store')
->andWhere('store = :store')
->setParameter('store', $store);
} else {
$qb->andWhere('option.store IS NULL');
}
return $qb
->getQuery()
->getOneOrNullResult();
}
}