Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix truncate on MySQL >= 5.5 #127

Open
wants to merge 9 commits into
base: 1.5.x
Choose a base branch
from
5 changes: 4 additions & 1 deletion lib/Doctrine/Common/DataFixtures/AbstractFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ public function setReference($name, $object)
/**
* Set the reference entry identified by $name
* and referenced to managed $object. If $name
* already is set, it overrides it
* already is set, it throws a
* BadMethodCallException exception
*
* @param string $name
* @param object $object - managed object
* @see Doctrine\Common\DataFixtures\ReferenceRepository::addReference
* @throws BadMethodCallException - if repository already has
* a reference by $name
* @return void
*/
public function addReference($name, $object)
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/Common/DataFixtures/FixtureInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface FixtureInterface
/**
* Load data fixtures with the passed EntityManager
*
* @param Doctrine\Common\Persistence\ObjectManager $manager
* @param ObjectManager $manager
*/
function load(ObjectManager $manager);
public function load(ObjectManager $manager);
}
6 changes: 4 additions & 2 deletions lib/Doctrine/Common/DataFixtures/ProxyReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
* Allow data fixture references and identities to be persisted when cached data fixtures
* are pre-loaded, for example, by LiipFunctionalTestBundle\Test\WebTestCase loadFixtures().
*
* @author Guilherme Blanco <[email protected]>
* @author Anthon Pang <[email protected]>
*/
class ProxyReferenceRepository extends ReferenceRepository
Expand Down Expand Up @@ -59,12 +60,13 @@ protected function getRealClass($className)
*/
public function serialize()
{
$unitOfWork = $this->getManager()->getUnitOfWork();
$simpleReferences = array();

foreach ($this->getReferences() as $name => $reference) {
$className = $this->getRealClass(get_class($reference));

$simpleReferences[$name] = array($className, $reference->getId());
$simpleReferences[$name] = array($className, $this->getIdentifier($reference, $unitOfWork));
}

$serializedData = json_encode(array(
Expand All @@ -90,7 +92,7 @@ public function unserialize($serializedData)
$name,
$this->getManager()->getReference(
$proxyReference[0], // entity class name
$proxyReference[1] // id
$proxyReference[1] // identifiers
)
);
}
Expand Down
23 changes: 23 additions & 0 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace Doctrine\Common\DataFixtures\Purger;

use Doctrine\DBAL\Platforms\MySqlPlatform;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Internal\CommitOrderCalculator;
use Doctrine\ORM\Mapping\ClassMetadata;
Expand Down Expand Up @@ -127,13 +128,21 @@ public function purge()
$orderedTables[] = $class->getQuotedTableName($platform);
}

if ($platform instanceof MySqlPlatform) {
$this->setForeignKeyChecks(false);
}

foreach($orderedTables as $tbl) {
if ($this->purgeMode === self::PURGE_MODE_DELETE) {
$this->em->getConnection()->executeUpdate("DELETE FROM " . $tbl);
} else {
$this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true));
}
}

if ($platform instanceof MySqlPlatform) {
$this->setForeignKeyChecks(true);
}
}

private function getCommitOrder(EntityManager $em, array $classes)
Expand Down Expand Up @@ -196,4 +205,18 @@ private function getAssociationTables(array $classes)

return $associationTables;
}

/**
* Enable/disable foreign key checks on the MySQL platform
*
* @param bool $bool
*/
private function setForeignKeyChecks($bool)
{
if ($this->purgeMode !== self::PURGE_MODE_TRUNCATE) {
return;
}

$this->em->getConnection()->query(sprintf('SET FOREIGN_KEY_CHECKS=%s', (int) $bool));
}
}
11 changes: 10 additions & 1 deletion lib/Doctrine/Common/DataFixtures/ReferenceRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,23 @@ public function __construct(ObjectManager $manager)
* @param object $reference Reference object
* @param object $uow Unit of work
*
* @return mixed
* @return array
*/
protected function getIdentifier($reference, $uow)
{
// In case Reference is not yet managed in UnitOfWork
if ( ! $uow->isInIdentityMap($reference)) {
$class = $this->manager->getClassMetadata(get_class($reference));

return $class->getIdentifierValues($reference);
}

// Dealing with ORM UnitOfWork
if (method_exists($uow, 'getEntityIdentifier')) {
return $uow->getEntityIdentifier($reference);
}

// ODM UnitOfWork
return $uow->getDocumentIdentifier($reference);
}

Expand Down