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

Quote table names that also are reserved keywords #334

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

use Doctrine\Common\DataFixtures\Sorter\TopologicalSorter;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\Identifier;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use function array_reverse;
use function array_search;
use function count;
use function implode;
use function is_callable;
use function method_exists;
use function preg_match;
Expand Down Expand Up @@ -246,11 +248,15 @@ private function getAssociationTables(array $classes, AbstractPlatform $platform
*/
private function getTableName($class, $platform)
{
if (isset($class->table['schema']) && ! method_exists($class, 'getSchemaName')) {
return $class->table['schema'] . '.' . $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
if (method_exists($class, 'getSchemaName')) {
$identifier[] = $class->getSchemaName();
} elseif (isset($class->table['schema'])) {
$identifier[] = $class->table['schema'];
}

return $this->em->getConfiguration()->getQuoteStrategy()->getTableName($class, $platform);
$identifier[] = $class->getTableName();

return (new Identifier(implode('.', $identifier)))->getQuotedName($platform);
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/Purger/ORMPurgerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ORMPurgerTest extends BaseTest
public const TEST_ENTITY_USER = TestEntity\User::class;
public const TEST_ENTITY_USER_WITH_SCHEMA = TestEntity\UserWithSchema::class;
public const TEST_ENTITY_QUOTED = TestEntity\Quoted::class;
public const TEST_ENTITY_GROUP = TestEntity\Group::class;

public function testGetAssociationTables()
{
Expand Down Expand Up @@ -54,4 +55,18 @@ public function testTableNameWithSchema()
$tableName = $method->invokeArgs($purger, [$metadata, $platform]);
$this->assertStringStartsWith('test_schema', $tableName);
}

public function testGetTableNameQuoted() : void
{
$em = $this->getMockAnnotationReaderEntityManager();
$metadata = $em->getClassMetadata(self::TEST_ENTITY_GROUP);
$platform = $em->getConnection()->getDatabasePlatform();
$purger = new ORMPurger($em);
$class = new ReflectionClass(ORMPurger::class);
$method = $class->getMethod('getTableName');
$method->setAccessible(true);
$tableName = $method->invokeArgs($purger, [$metadata, $platform]);
$this->assertStringStartsWith('"', $tableName);
SenseException marked this conversation as resolved.
Show resolved Hide resolved
$this->assertStringEndsWith('"', $tableName);
}
}
49 changes: 49 additions & 0 deletions tests/Doctrine/Tests/Common/DataFixtures/TestEntity/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\Common\DataFixtures\TestEntity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
*/
class Group
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
*
* @var int|null
*/
private $id;

/**
* @ORM\Column(length=32)
* @ORM\Id
*
* @var string|null
*/
private $code;

public function setId($id) : void
{
$this->id = $id;
}

public function getId() : ?int
{
return $this->id;
}

public function setCode($code) : void
{
$this->code = $code;
}

public function getCode() : ?string
{
return $this->code;
}
}