Skip to content

Instantly share code, notes, and snippets.

@kaystrobach
Created April 15, 2025 13:42
Show Gist options
  • Save kaystrobach/67d127ce2651bf703203327d6b692b19 to your computer and use it in GitHub Desktop.
Save kaystrobach/67d127ce2651bf703203327d6b692b19 to your computer and use it in GitHub Desktop.
Flow Doctrine Migration with entitymanager etc. without validation
<?php
class Version20250403102435 extends AbstractMigration
{
protected ObjectManager $objectManager;
protected PersistenceManager $persistenceManager;
protected EntityManagerInterface $entityManager;
protected Output $output;
public function up(Schema $schema): void
{
// Boot the Flow application
$bootstrap = Bootstrap::$staticObjectManager->get(Bootstrap::class);
// Retrieve the object manager
$this->objectManager = $bootstrap->getObjectManager();
$this->persistenceManager = $this->objectManager->get(PersistenceManager::class);
$this->entityManager = $this->objectManager->get(EntityManagerInterface::class);
$this->output = $this->objectManager->get(ConsoleOutput::class);
// [...]
$this->wrapNoValidation(
function () use ($pruefungsTerminRepository, $termin) {
$pruefungsTerminRepository->add($termin);
$this->persistenceManager->persistAll();
}
);
}
public function wrapNoValidation(callable $callback)
{
// Temporär deaktivieren wir Doctrine Events (Validation):
$eventManager = $this->entityManager->getEventManager();
$originalListeners = $eventManager->getListeners();
try {
// Entferne alle eventuellen Validierungs-Event-Listener
foreach ($originalListeners as $event => $listeners) {
foreach ($listeners as $listener) {
$eventManager->removeEventListener($event, $listener);
}
}
// Führe die Operation aus
return $callback();
} finally {
// Validierungs-Events wieder aktivieren
foreach ($originalListeners as $event => $listeners) {
foreach ($listeners as $listener) {
$eventManager->addEventListener($event, $listener);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment