Skip to content

Instantly share code, notes, and snippets.

@merk
Last active December 11, 2015 19:58
Show Gist options
  • Save merk/4652111 to your computer and use it in GitHub Desktop.
Save merk/4652111 to your computer and use it in GitHub Desktop.
Truncate workaround for doctrine fixtures
<?php
namespace Ibms\AppBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\Persistence\ObjectManager;
use Infinite\Helper\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
/**
* This fixture is used to change the database connection to
* ignore foreign key checks and truncate.
*
* Make sure this is the first fixture in your fixtures, and run
* the command with --append, ie
* app/console doctrine:fixtures:load --append
*
* @author Tim Nagel <[email protected]>
*/
class TruncateFixture extends AbstractFixture implements ContainerAwareInterface, OrderedFixtureInterface
{
use ContainerAwareTrait;
/**
* Load data fixtures with the passed EntityManager
*
* @param \Doctrine\Common\Persistence\ObjectManager|\Doctrine\ORM\EntityManager $manager
*/
public function load(ObjectManager $manager)
{
$connection = $manager->getConnection();
if ('pdo_mysql' !== $connection->getDriver()->getName()) {
return;
}
$connection->executeQuery('SET FOREIGN_KEY_CHECKS=0');
$purger = new ORMPurger($manager);
$purger->setPurgeMode($purger::PURGE_MODE_TRUNCATE);
$purger->purge();
$connection->executeQuery('SET FOREIGN_KEY_CHECKS=1');
}
public function getOrder()
{
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment