Skip to content

Commit

Permalink
[DoctrineBundle] Fixed bug in data:load when purging many-to-many rel…
Browse files Browse the repository at this point in the history
…ationships
  • Loading branch information
Brandon Turner authored and fabpot committed Aug 31, 2010
1 parent 2731979 commit 935ac56
Showing 1 changed file with 35 additions and 8 deletions.
Expand Up @@ -13,6 +13,7 @@
use Doctrine\Common\Cli\CliController as DoctrineCliController;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Internal\CommitOrderCalculator;
use Doctrine\ORM\Mapping\ClassMetadata;

/*
* This file is part of the Symfony framework.
Expand Down Expand Up @@ -142,15 +143,26 @@ protected function purgeEntityManager(EntityManager $em)
$classes[] = $metadata;
}
}
$cmf = $em->getMetadataFactory();
$classes = $this->getCommitOrder($em, $classes);
for ($i = count($classes) - 1; $i >= 0; --$i) {
$class = $classes[$i];
if ($cmf->hasMetadataFor($class->name)) {
try {
$em->createQuery('DELETE FROM '.$class->name.' a')->execute();
} catch (Exception $e) {}

$commitOrder = $this->getCommitOrder($em, $classes);

// Drop association tables first
$orderedTables = $this->getAssociationTables($commitOrder);

// Drop tables in reverse commit order
for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
$class = $commitOrder[$i];

if (($class->isInheritanceTypeSingleTable() && $class->name != $class->rootEntityName)
|| $class->isMappedSuperclass) {
continue;
}

$orderedTables[] = $class->getTableName();
}

foreach($orderedTables as $tbl) {
$em->getConnection()->executeUpdate("DELETE FROM $tbl");
}
}

Expand All @@ -177,4 +189,19 @@ protected function getCommitOrder(EntityManager $em, array $classes)

return $calc->getCommitOrder();
}

protected function getAssociationTables(array $classes)
{
$associationTables = array();

foreach ($classes as $class) {
foreach ($class->associationMappings as $assoc) {
if ($assoc['isOwningSide'] && $assoc['type'] == ClassMetadata::MANY_TO_MANY) {
$associationTables[] = $assoc['joinTable']['name'];
}
}
}

return $associationTables;
}
}

0 comments on commit 935ac56

Please sign in to comment.