Skip to content

Commit

Permalink
Merge pull request #180 from lavoiesl/ORMPurger-QuoteJoinTableName
Browse files Browse the repository at this point in the history
Fix Quoting of "joinTable" table names in ORMPurger
  • Loading branch information
lavoiesl committed Mar 23, 2015
2 parents 63ab93d + fa081fb commit 1d3baab
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 9 deletions.
20 changes: 13 additions & 7 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Internal\CommitOrderCalculator;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* Class responsible for purging databases of data before reloading data fixtures.
Expand Down Expand Up @@ -109,12 +110,12 @@ public function purge()

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

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

// Get platform parameters
$platform = $this->em->getConnection()->getDatabasePlatform();

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

// Drop tables in reverse commit order
for ($i = count($commitOrder) - 1; $i >= 0; --$i) {
$class = $commitOrder[$i];
Expand Down Expand Up @@ -185,17 +186,22 @@ private function getCommitOrder(EntityManagerInterface $em, array $classes)
return $calc->getCommitOrder();
}

private function getAssociationTables(array $classes)
/**
* @param array $classes
* @param \Doctrine\DBAL\Platforms\AbstractPlatform $platform
* @return array
*/
private function getAssociationTables(array $classes, AbstractPlatform $platform)
{
$associationTables = array();

foreach ($classes as $class) {
foreach ($class->associationMappings as $assoc) {
if ($assoc['isOwningSide'] && $assoc['type'] == ClassMetadata::MANY_TO_MANY) {
if (isset($assoc['joinTable']['schema']) && null !== $assoc['joinTable']['schema']) {
$associationTables[] = $assoc['joinTable']['schema'] . '.' . $assoc['joinTable']['name'];
if (isset($assoc['joinTable']['schema'])) {
$associationTables[] = $assoc['joinTable']['schema'] . '.' . $class->getQuotedJoinTableName($assoc, $platform);
} else {
$associationTables[] = $assoc['joinTable']['name'];
$associationTables[] = $class->getQuotedJoinTableName($assoc, $platform);
}
}
}
Expand Down
18 changes: 16 additions & 2 deletions tests/Doctrine/Tests/Common/DataFixtures/Purger/ORMPurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,31 @@
class ORMPurgerTest extends BaseTest
{
const TEST_ENTITY_USER = 'Doctrine\Tests\Common\DataFixtures\TestEntity\User';
const TEST_ENTITY_QUOTED = 'Doctrine\Tests\Common\DataFixtures\TestEntity\Quoted';

public function testGetAssociationTables()
{
$em = $this->getMockAnnotationReaderEntityManager();
$metadata = $em->getClassMetadata(self::TEST_ENTITY_USER);
$platform = $em->getConnection()->getDatabasePlatform();
$purger = new ORMPurger();
$class = new ReflectionClass('Doctrine\Common\DataFixtures\Purger\ORMPurger');
$method = $class->getMethod('getAssociationTables');
$method->setAccessible(true);
$associationTables = $method->invokeArgs($purger, array(array($metadata)));
$method->setAccessible(false);
$associationTables = $method->invokeArgs($purger, array(array($metadata), $platform));
$this->assertEquals($associationTables[0], 'readers.author_reader');
}

public function testGetAssociationTablesQuoted()
{
$em = $this->getMockAnnotationReaderEntityManager();
$metadata = $em->getClassMetadata(self::TEST_ENTITY_QUOTED);
$platform = $em->getConnection()->getDatabasePlatform();
$purger = new ORMPurger();
$class = new ReflectionClass('Doctrine\Common\DataFixtures\Purger\ORMPurger');
$method = $class->getMethod('getAssociationTables');
$method->setAccessible(true);
$associationTables = $method->invokeArgs($purger, array(array($metadata), $platform));
$this->assertEquals($associationTables[0], '"INSERT"');
}
}
31 changes: 31 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Quoted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Doctrine\Tests\Common\DataFixtures\TestEntity;

/**
* @Entity
* @Table("`SELECT`")
*/
class Quoted
{
/**
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;

/**
* @Column(length=50, name="select")
*/
private $select;

/**
* @ManyToMany(targetEntity="Quoted")
* @JoinTable(name="`INSERT`",
* joinColumns={@JoinColumn(name="`SELECT`", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="`UPDATE`", referencedColumnName="id")}
* )
*/
private $selects;
}

0 comments on commit 1d3baab

Please sign in to comment.