2023-04-15 00:43:55 +02:00

63 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\CustomFieldsBundle\EntityRepository\CustomFieldLongChoice;
use Chill\CustomFieldsBundle\Entity\CustomFieldLongChoice\Option;
use Doctrine\ORM\EntityRepository;
class OptionRepository extends EntityRepository
{
/**
* @param string $key
* @param mixed $includeParents
* @param mixed $active
*
* @return Option[]
*/
public function findFilteredByKey($key, $includeParents = true, $active = true)
{
$qb = $this->createQueryBuilder('option');
$qb->where('option.key = :key');
if (true === $active) {
$qb->andWhere('option.active = true');
}
if (false === $includeParents) {
$qb->andWhere('option.parent IS NOT NULL');
if (true === $active) {
$qb->join('option.parent', 'p');
$qb->andWhere('p.active = true');
}
}
$qb->setParameter('key', $key);
return $qb->getQuery()->getResult();
}
/**
* @return string[]
*/
public function getKeys()
{
$keys = $this->createQueryBuilder('option')
->select('option.key')
->distinct()
->getQuery()
->getScalarResult();
return array_map(static fn ($r) => $r['key'], $keys);
}
}