Skip to content

Instantly share code, notes, and snippets.

@Nek-
Created February 19, 2013 22:59
Show Gist options
  • Save Nek-/4991033 to your computer and use it in GitHub Desktop.
Save Nek-/4991033 to your computer and use it in GitHub Desktop.
From #[email protected] How to use fixtures with MySQL and fix Constrains problems
<?php
namespace Otera\SiteBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
/**
* Geek Otera Project
*
* 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
{
/**
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
private $container;
/**
* Sets the Container.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container A ContainerInterface instance
*
* @api
*/
public function setContainer(\Symfony\Component\DependencyInjection\ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* 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