src/EventSubscriber/FormFieldRuleSubscriber.php line 17
<?phpnamespace App\EventSubscriber;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Doctrine\ORM\Events;use Doctrine\ORM\Event\OnFlushEventArgs;use Doctrine\ORM\EntityManagerInterface;use Doctrine\ORM\UnitOfWork;use App\Entity\FormField;use App\Document\Action;use App\Document\Condition;class FormFieldRuleSubscriber implements EventSubscriberInterface{public function onFlush(OnFlushEventArgs $args): void{$em = $args->getEntityManager();$uow = $em->getUnitOfWork();foreach ($uow->getScheduledEntityUpdates() as $entity) {if (!$entity instanceof FormField) {continue;}$changeSet = $uow->getEntityChangeSet($entity);$this->handleFormFieldChange($entity, $changeSet, $em, $uow);}}private function handleFormFieldChange(FormField $field,array $changeSet,EntityManagerInterface $em,UnitOfWork $uow): void {if (!isset($changeSet['multiple'])) {return;}[$old, $new] = $changeSet['multiple'];if ($old === true && $new === false) {$this->fixRulesForMultipleToSingle($field, $em, $uow);}}private function fixRulesForMultipleToSingle(FormField $field,EntityManagerInterface $em,UnitOfWork $uow): void {$fieldId = $field->getId();$actions = $this->dm->getRepository(Action::class)->findBy(['formField' => $fieldId]);foreach ($actions as $action) {$value = $action->getValue();if (is_array($value)) {// Keep first value$action->setValue(reset($value));$this->dm->persist($action);}}$conditions = $this->dm->getRepository(Condition::class)->findBy(['formField' => $fieldId]);foreach ($conditions as $condition) {$value = $condition->getValue();if (is_array($value)) {// Keep first value$condition->setValue(reset($value));// multiple → false => AND/OR no longer meaningful$condition->setAndOrValue(null);$this->dm->persist($condition);}}}public static function getSubscribedEvents(): array{return [Events::onFlush];}}