Showing posts with label doctrine-fixtures. Show all posts
Showing posts with label doctrine-fixtures. Show all posts

Saturday, 3 March 2012

Handling the doctrine-fixtures interface update error

Got the following error after updating doctrine-fixtures:
Fatal error: Declaration of Translator\GFBundle\DataFixtures\ORM\StatementTranslationFixtures::load() must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load()

It turns out that doctrine-fixtures changed their interface for the load function from:
 interface FixtureInterface
    {
        load($manager);
    }


To:
use Doctrine\Common\Persistence\ObjectManager;

    interface FixtureInterface
    {
        load(ObjectManager $manager);
    }



... as seen in the included text file named "UPGRADE".


Solution:


To update your fixture files, simply add the "use" line in your fixtures file, and change all of your load() functions to include the ObjectManager bit.


Example:


... (other use statements) ...
load($manager){
  ... (code to load stuff ) ...
}


To:
... (other use statements) ...
use Doctrine\Common\Persistence\ObjectManager;

load(ObjectManager $manager){
  ... (code to load stuff) ...
}

Symfony 2 Doctrine Fixtures TRUNCATE (rather than DELETE) hack

Symfony2 fixtures clears database tables prior to populating them with fixture data. In the (earlier) version of doctrine-fixtures we tried, tables would refuse to be cleared if there were foreign keys, or indexes would not reset due to using SQL DELETE versus TRUNCATE.

This behavior might be fixed by now, but just in case you have an older version, here's a quick hack around this problem.

Warning: don't do this hack if you're not sure what you're doing! These instructions aren't comprehensive. They're only complete enough for someone to figure out why the changes should work.

1) locate your ORM Purger
it should be in:
vendors/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php

2) make the purge() function truncate
There should be some loop in purge() that looks like:
foreach($orderedTables as $tbl) {
            ... (old code for deleting tables) ...
}


update this to be something like the following, commenting out old code so you can undo if you messed up:
foreach($orderedTables as $tbl) {
  // stupid hack
  $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 0;");
  $this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true));
  $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 1;");
}


The disabling of the foreign key checks is done so the tables can be truncated, otherwise SQL will not allow the truncate operation if your database have foreign key constraints set.


3) how to undo
Un-comment the old code if you commented it out, or reinstall doctrine-fixtures.


4) final thoughts
This isn't the best way around this doctrine-fixtures behavior, and it's only meant as a hack for the impatient.