entityManager = $entityManager; $this->demandeRepository = $demandeRepository; $this->placeRepository = $placeRepository; } protected function configure(): void { $this ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Show matches without linking'); } protected function execute(InputInterface $input, OutputInterface $output): int { $io = new SymfonyStyle($input, $output); $io->title('Linking Demandes to Places based on matching OSM type and ID'); $dryRun = $input->getOption('dry-run'); // Find all Demandes without a UUID but with OSM type and ID $demandesWithoutUuid = $this->demandeRepository->createQueryBuilder('d') ->where('d.placeUuid IS NULL') ->andWhere('d.osmObjectType IS NOT NULL') ->andWhere('d.osmId IS NOT NULL') ->getQuery() ->getResult(); if (empty($demandesWithoutUuid)) { $io->warning('No Demandes without UUID but with OSM type and ID found.'); return Command::SUCCESS; } $io->info(sprintf('Found %d Demandes without UUID but with OSM type and ID.', count($demandesWithoutUuid))); // Process each Demande $linkedCount = 0; /** @var Demande $demande */ foreach ($demandesWithoutUuid as $demande) { $osmType = $demande->getOsmObjectType(); $osmId = $demande->getOsmId(); // Find Place with matching OSM type and ID $place = $this->placeRepository->findOneBy([ 'osm_kind' => $osmType, 'osmId' => $osmId ]); if ($place) { $io->text(sprintf( 'Match found: Demande #%d -> Place #%d (OSM %s/%d)', $demande->getId(), $place->getId(), $osmType, $osmId )); if (!$dryRun) { $demande->setPlace($place); $demande->setPlaceUuid($place->getUuidForUrl()); $demande->setStatus('linked_to_place'); $this->entityManager->persist($demande); $linkedCount++; } } else { $io->text(sprintf( 'No matching Place found for Demande #%d (OSM %s/%d)', $demande->getId(), $osmType, $osmId )); } } if (!$dryRun && $linkedCount > 0) { $this->entityManager->flush(); $io->success(sprintf('Linked %d Demandes to Places based on OSM type and ID.', $linkedCount)); } elseif ($dryRun) { $io->info('Dry run completed. No changes were made.'); } else { $io->info('No Demandes were linked to Places.'); } return Command::SUCCESS; } }