|
4 | 4 |
|
5 | 5 | use App\Entity\Answer; |
6 | 6 | use App\Entity\Question; |
| 7 | +use App\Entity\Report; |
7 | 8 | use App\Entity\System; |
8 | 9 | use Doctrine\ORM\EntityManagerInterface; |
9 | 10 | use EasyCorp\Bundle\EasyAdminBundle\Config\Action; |
@@ -59,24 +60,28 @@ protected function getRedirectResponseAfterSave(AdminContext $context, string $a |
59 | 60 | #[\Override] |
60 | 61 | public function createEntity(string $entityFqcn): Answer |
61 | 62 | { |
62 | | - /** @var Answer $entity */ |
63 | | - $entity = parent::createEntity($entityFqcn); |
64 | | - |
65 | | - // Set system and question on new answers. |
| 63 | + // Set question and report OR system on new answers. |
66 | 64 | $params = $this->getContext()->getRequest()->query->all(); |
67 | | - if (isset($params['system'], $params['question'])) { |
68 | | - $system = $this->entityManager->getRepository(System::class)->find($params['system']); |
69 | | - $question = $this->entityManager->getRepository(Question::class)->find($params['question']); |
70 | | - if ($system && $question) { |
71 | | - $entity |
72 | | - ->setSystem($system) |
73 | | - ->setQuestion($question); |
74 | | - } else { |
75 | | - throw new BadRequestHttpException(); |
76 | | - } |
| 65 | + // Question is required. |
| 66 | + $question = isset($params['question']) ? $this->entityManager->getRepository(Question::class)->find($params['question']) : null; |
| 67 | + if (null === $question) { |
| 68 | + throw new BadRequestHttpException('Missing or invalid question'); |
77 | 69 | } |
| 70 | + // One or report or system is required. |
| 71 | + $report = isset($params['report']) ? $this->entityManager->getRepository(Report::class)->find($params['report']) : null; |
| 72 | + $system = isset($params['system']) ? $this->entityManager->getRepository(System::class)->find($params['system']) : null; |
| 73 | + |
| 74 | + if (null === $report && null === $system) { |
| 75 | + throw new BadRequestHttpException('Either report or system must be set'); |
| 76 | + } |
| 77 | + |
| 78 | + /** @var Answer $entity */ |
| 79 | + $entity = parent::createEntity($entityFqcn); |
78 | 80 |
|
79 | | - return $entity; |
| 81 | + return $entity |
| 82 | + ->setQuestion($question) |
| 83 | + ->setReport($report) |
| 84 | + ->setSystem($system); |
80 | 85 | } |
81 | 86 |
|
82 | 87 | #[\Override] |
|
0 commit comments