| 1: | <?php |
| 2: | namespace TIC\DormBundle\Traits; |
| 3: | |
| 4: | use Doctrine\ORM\EntityManagerInterface; |
| 5: | use Doctrine\ORM\EntityRepository; |
| 6: | |
| 7: | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | trait ManagerTrait |
| 14: | { |
| 15: | |
| 16: | use \TIC\CoreBundle\Traits\ContextProperties; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | protected $em; |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | public function setEntityManager(EntityManagerInterface $manager): void |
| 31: | { |
| 32: | $this->em = $manager; |
| 33: | } |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | public function EmDisableLogger(): void |
| 39: | { |
| 40: | $this->em->getConnection()->getConfiguration()->setSQLLogger(null); |
| 41: | } |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | public function EmDisableFilter(string $filter): bool |
| 47: | { |
| 48: | |
| 49: | $filters = $this->em->getFilters(); |
| 50: | if (\array_key_exists($filter, $filters->getEnabledFilters())) { |
| 51: | $filters->disable($filter); |
| 52: | return true; |
| 53: | } |
| 54: | return false; |
| 55: | } |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | public function EmEnableFilter(string $filter): bool |
| 61: | { |
| 62: | |
| 63: | $filters = $this->em->getFilters(); |
| 64: | if (\array_key_exists($filter, $filters->getDisabledFilters())) { |
| 65: | $filters->enable($filter); |
| 66: | return true; |
| 67: | } |
| 68: | return false; |
| 69: | } |
| 70: | |
| 71: | |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | protected function getRepo(?string $itemName = null): EntityRepository |
| 79: | { |
| 80: | if ($itemName === null) |
| 81: | $entityClass = $this->ctxRoot . "\\Entity\\" . $this->ctxName; |
| 82: | elseif (!str_contains($itemName, "\\")) |
| 83: | $entityClass = $this->ctxRoot . "\\Entity\\" . \ucfirst($itemName); |
| 84: | else |
| 85: | $entityClass = $itemName; |
| 86: | |
| 87: | return $this->em->getRepository($entityClass); |
| 88: | } |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | protected function getItem(mixed $primary = null, ?bool $no_exception = false): ?object |
| 98: | { |
| 99: | |
| 100: | if (null === $primary) { |
| 101: | $entityClass = $this->ctxRoot . "\\Entity\\" . $this->ctxName; |
| 102: | return new $entityClass(); |
| 103: | } |
| 104: | |
| 105: | |
| 106: | $entity = $this->getRepo()->getItem($primary); |
| 107: | if (\is_object($entity)) return $entity; |
| 108: | |
| 109: | |
| 110: | if (empty($no_exception)) { |
| 111: | $message = 'error.' . $this->ctxMesg . '.object_not_found'; |
| 112: | if (\method_exists($this, "trans")) $message = $this->trans($message); |
| 113: | |
| 114: | throw new NotFoundHttpException($message); |
| 115: | } |
| 116: | return null; |
| 117: | } |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | |
| 124: | |
| 125: | protected function saveEntity(object $entity): ?string |
| 126: | { |
| 127: | try { |
| 128: | |
| 129: | $this->em->persist($entity); |
| 130: | $this->em->flush(); |
| 131: | } catch (\Exception $e) { |
| 132: | return $e->getMessage(); |
| 133: | } |
| 134: | return null; |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | protected function deleteEntity(object $entity): ?string |
| 144: | { |
| 145: | try { |
| 146: | |
| 147: | $this->em->remove($entity); |
| 148: | $this->em->flush(); |
| 149: | } catch (\Exception $e) { |
| 150: | return $e->getMessage(); |
| 151: | } |
| 152: | return null; |
| 153: | } |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | |
| 159: | |
| 160: | |
| 161: | protected function harddeleteEntity(object $entity): ?string |
| 162: | { |
| 163: | $hasFilter = $this->EmDisableFilter('softdeleteable'); |
| 164: | $rc = $this->deleteEntity($entity); |
| 165: | |
| 166: | if ($hasFilter) { |
| 167: | $this->EmEnableFilter('softdeleteable'); |
| 168: | if ($rc !== null) return $this->deleteEntity($entity); |
| 169: | } |
| 170: | |
| 171: | return $rc; |
| 172: | } |
| 173: | |
| 174: | } |
| 175: | |