Created
March 4, 2015 09:55
-
-
Save Ocramius/96206e3b39e96bd64bc5 to your computer and use it in GitHub Desktop.
Brutal ORM Purger for doctrine/data-fixtures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Depends on your setup: in my case, just pass it to the constructor of the fixture loader when you instantiate it.