Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ocramius/96206e3b39e96bd64bc5 to your computer and use it in GitHub Desktop.
Save Ocramius/96206e3b39e96bd64bc5 to your computer and use it in GitHub Desktop.
Brutal ORM Purger for doctrine/data-fixtures
<?php
namespace FixturesStuff
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\ORM\EntityManagerInterface;
/**
* @TODO this has to go once we implement correct purging of entities
*/
class BrutalForeignKeyDisablingMySQLPurger extends ORMPurger
{
/**
* @var EntityManagerInterface
*/
private $entityManager;
/**
* @var ORMPurger
*/
private $purger;
/**
* @param EntityManagerInterface $entityManager
* @param ORMPurger $purger
*/
public function __construct(EntityManagerInterface $entityManager, ORMPurger $purger)
{
$this->entityManager = $entityManager;
$this->purger = $purger;
}
/**
* {@inheritDoc}
*/
public function setPurgeMode($mode)
{
$this->purger->setPurgeMode($mode);
}
/**
* {@inheritDoc}
*/
public function getPurgeMode()
{
return $this->purger->getPurgeMode();
}
/**
* {@inheritDoc}
*/
public function setEntityManager(EntityManagerInterface $em)
{
$this->purger->setEntityManager($em);
$this->entityManager = $em;
}
/**
* {@inheritDoc}
*/
public function getObjectManager()
{
return $this->purger->getObjectManager();
}
/**
* {@inheritDoc}
*/
function purge()
{
$connection = $this->entityManager->getConnection();
try {
$connection->executeQuery('SET FOREIGN_KEY_CHECKS = 0');
$this->purger->purge();
} finally {
$connection->executeQuery('SET FOREIGN_KEY_CHECKS = 1');
}
}
}
@klodoma
Copy link

klodoma commented Oct 28, 2020

How do you hook this in so that this purger is used instead of the ORMPurger?

@Ocramius
Copy link
Author

Depends on your setup: in my case, just pass it to the constructor of the fixture loader when you instantiate it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment