Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@xrstf
Created September 14, 2012 12:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xrstf/3721543 to your computer and use it in GitHub Desktop.
Save xrstf/3721543 to your computer and use it in GitHub Desktop.
Load Doctrine2 fixtures with foreign keys disabled
<?php
use Doctrine\Common\DataFixtures\Loader;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase as SfWebTestCase;
use Project\DummyBundle\DataFixtures\ORM\MyFixture;
abstract class WebTestCase extends SfWebTestCase {
public function setUp() {
// prepare entity manager
$client = static::createClient();
$container = $client->getContainer();
$em = $container->get('doctrine')->getEntityManager();
// make sure foreign keys don't fuck up the TRUNCATE queries
$em->getConnection()->exec('SET foreign_key_checks = 0');
// load our fixture (have to inject the container manually... don't know why, but the above query seems to break it)
$fixture = new MyFixture();
$loader = new Loader();
$fixture->setContainer($container);
$loader->addFixture($fixture);
// execute the fixture
$purger = new ORMPurger($em);
$executor = new ORMExecutor($em, $purger);
$executor->execute($loader->getFixtures());
// and revert the setting
$em->getConnection()->exec('SET foreign_key_checks = 1');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment