| 1: | <?php |
| 2: | namespace TIC\DormBundle\Base; |
| 3: | |
| 4: | use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; |
| 5: | use Doctrine\Persistence\ManagerRegistry; |
| 6: | use Doctrine\ORM\EntityRepository; |
| 7: | use Doctrine\ORM\QueryBuilder; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | abstract class TICRepository extends ServiceEntityRepository |
| 18: | { |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | use \TIC\DormBundle\Traits\RepositoryFilterable; |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | public function __construct(ManagerRegistry $registry) |
| 29: | { |
| 30: | $entityClass = \str_replace("\\Repository\\", "\\Entity\\", static::class); |
| 31: | $entityClass = \preg_replace('/Repository$/', '', $entityClass); |
| 32: | parent::__construct($registry, $entityClass); |
| 33: | } |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | protected function getRepo(string $entity): EntityRepository |
| 42: | { |
| 43: | $entitySpace = \preg_match('/^(.+\\\\Entity\\\\)/', $this->_entityName, $m) ? $m[1] : ''; |
| 44: | return $this->_em->getRepository($entitySpace . \ucfirst($entity)); |
| 45: | } |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | public function getItem($id): ?object |
| 55: | { |
| 56: | return $this->find($id); |
| 57: | } |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | |
| 65: | |
| 66: | |
| 67: | public function getFieldValues(string $field, string $term = '', int $minOccurs = 0): array |
| 68: | { |
| 69: | $field = 'a.' . $field; |
| 70: | if (! $minOccurs) $qb = $this->createQueryBuilder('a') |
| 71: | ->select('DISTINCT(' . $field . ')') |
| 72: | ->where($field . ' IS NOT NULL')->andWhere($field . ' != \'\'') |
| 73: | ->orderBy($field, 'ASC') |
| 74: | ; |
| 75: | else $qb = $this->createQueryBuilder('a') |
| 76: | ->select($field)->groupBy($field) |
| 77: | ->where($field . ' IS NOT NULL')->andWhere($field . ' != \'\'') |
| 78: | ->having('COUNT(' . $field . ') >= :min')->setParameter('min', $minOccurs) |
| 79: | ->orderBy($field, 'ASC') |
| 80: | ; |
| 81: | if ($term !== '') $qb |
| 82: | ->andWhere($field . ' LIKE :term') |
| 83: | ->setParameter('term', '%' . $term . '%') |
| 84: | ; |
| 85: | return $qb->getQuery()->getResult('column'); |
| 86: | } |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | |
| 98: | |
| 99: | |
| 100: | public function genUniqueRandom(string $field, int $length = 8, string $format = null, mixed $sigils = null): string |
| 101: | { |
| 102: | |
| 103: | $max_try = 1000; |
| 104: | while (true) { |
| 105: | |
| 106: | $str = \TIC\CoreBundle\Util\StringHelper::genrandom($length, $sigils); |
| 107: | if (\is_string($format) && (\strpos($format, '%s') !== false)) $str = \sprintf($format, $str); |
| 108: | |
| 109: | |
| 110: | $res = $this->createQueryBuilder('a')->select('COUNT(a)') |
| 111: | ->where('a.' . $field . ' = :str')->setParameter('str', $str) |
| 112: | ->getQuery()->getSingleScalarResult(); |
| 113: | if (! $res) break; |
| 114: | if (--$max_try < 1) throw new \Exception("Can't generate unique random value for " . $field); |
| 115: | } |
| 116: | return $str; |
| 117: | } |
| 118: | |
| 119: | } |
| 120: | |