1
0
mirror of synced 2025-02-02 21:41:45 +03:00

Fix applying collation on foreign key columns

This commit is contained in:
Tobias Schultze 2017-11-19 00:57:54 +01:00 committed by Luís Cobucci
parent f80656cddf
commit ee8dc496d9
No known key found for this signature in database
GPG Key ID: EC61C5F01750ED3C
3 changed files with 63 additions and 17 deletions

View File

@ -45,6 +45,8 @@ use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
*/ */
class SchemaTool class SchemaTool
{ {
private const KNOWN_COLUMN_OPTIONS = ['comment', 'unsigned', 'fixed', 'default'];
/** /**
* @var \Doctrine\ORM\EntityManagerInterface * @var \Doctrine\ORM\EntityManagerInterface
*/ */
@ -467,19 +469,8 @@ class SchemaTool
$options['columnDefinition'] = $mapping['columnDefinition']; $options['columnDefinition'] = $mapping['columnDefinition'];
} }
if (isset($mapping['options'])) { // the 'default' option can be overwritten here
$knownOptions = ['comment', 'unsigned', 'fixed', 'default']; $options = $this->gatherColumnOptions($mapping) + $options;
foreach ($knownOptions as $knownOption) {
if (array_key_exists($knownOption, $mapping['options'])) {
$options[$knownOption] = $mapping['options'][$knownOption];
unset($mapping['options'][$knownOption]);
}
}
$options['customSchemaOptions'] = $mapping['options'];
}
if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == [$mapping['fieldName']]) { if ($class->isIdGeneratorIdentity() && $class->getIdentifierFieldNames() == [$mapping['fieldName']]) {
$options['autoincrement'] = true; $options['autoincrement'] = true;
@ -690,9 +681,7 @@ class SchemaTool
$columnOptions['notnull'] = ! $joinColumn['nullable']; $columnOptions['notnull'] = ! $joinColumn['nullable'];
} }
if (isset($fieldMapping['options'])) { $columnOptions = $columnOptions + $this->gatherColumnOptions($fieldMapping);
$columnOptions['options'] = $fieldMapping['options'];
}
if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) { if ($fieldMapping['type'] == "string" && isset($fieldMapping['length'])) {
$columnOptions['length'] = $fieldMapping['length']; $columnOptions['length'] = $fieldMapping['length'];
@ -745,6 +734,23 @@ class SchemaTool
} }
} }
/**
* @param mixed[] $mapping
*
* @return mixed[]
*/
private function gatherColumnOptions(array $mapping) : array
{
if (! isset($mapping['options'])) {
return [];
}
$options = array_intersect_key($mapping['options'], array_flip(self::KNOWN_COLUMN_OPTIONS));
$options['customSchemaOptions'] = array_diff_key($mapping['options'], $options);
return $options;
}
/** /**
* Drops the database schema for the given classes. * Drops the database schema for the given classes.
* *

View File

@ -9,7 +9,7 @@ namespace Doctrine\Tests\Models\Forum;
class ForumCategory class ForumCategory
{ {
/** /**
* @Column(type="integer") * @Column(type="string", length=8, options={"fixed":true, "collation":"latin1_bin", "foo":"bar"})
* @Id * @Id
*/ */
private $id; private $id;

View File

@ -18,6 +18,8 @@ use Doctrine\Tests\Models\CompositeKeyInheritance\JoinedDerivedChildClass;
use Doctrine\Tests\Models\CompositeKeyInheritance\JoinedDerivedIdentityClass; use Doctrine\Tests\Models\CompositeKeyInheritance\JoinedDerivedIdentityClass;
use Doctrine\Tests\Models\CompositeKeyInheritance\JoinedDerivedRootClass; use Doctrine\Tests\Models\CompositeKeyInheritance\JoinedDerivedRootClass;
use Doctrine\Tests\Models\Forum\ForumAvatar; use Doctrine\Tests\Models\Forum\ForumAvatar;
use Doctrine\Tests\Models\Forum\ForumBoard;
use Doctrine\Tests\Models\Forum\ForumCategory;
use Doctrine\Tests\Models\Forum\ForumUser; use Doctrine\Tests\Models\Forum\ForumUser;
use Doctrine\Tests\Models\NullDefault\NullDefaultColumn; use Doctrine\Tests\Models\NullDefault\NullDefaultColumn;
use Doctrine\Tests\OrmTestCase; use Doctrine\Tests\OrmTestCase;
@ -86,6 +88,44 @@ class SchemaToolTest extends OrmTestCase
$this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition()); $this->assertEquals($customColumnDef, $table->getColumn('avatar_id')->getColumnDefinition());
} }
public function testPassColumnOptionsToJoinColumn()
{
$em = $this->_getTestEntityManager();
$schemaTool = new SchemaTool($em);
$category = $em->getClassMetadata(ForumCategory::class);
$board = $em->getClassMetadata(ForumBoard::class);
$classes = [$category, $board];
$schema = $schemaTool->getSchemaFromMetadata($classes);
self::assertTrue($schema->hasTable('forum_categories'));
self::assertTrue($schema->hasTable('forum_boards'));
$tableCategory = $schema->getTable('forum_categories');
$tableBoard = $schema->getTable('forum_boards');
self::assertTrue($tableBoard->hasColumn('category_id'));
self::assertSame(
$tableCategory->getColumn('id')->getFixed(),
$tableBoard->getColumn('category_id')->getFixed(),
'Foreign key/join column should have the same value of option `fixed` as the referenced column'
);
self::assertEquals(
$tableCategory->getColumn('id')->getCustomSchemaOptions(),
$tableBoard->getColumn('category_id')->getCustomSchemaOptions(),
'Foreign key/join column should have the same custom options as the referenced column'
);
self::assertEquals(
['collation' => 'latin1_bin', 'foo' => 'bar'],
$tableBoard->getColumn('category_id')->getCustomSchemaOptions()
);
}
/** /**
* @group DDC-283 * @group DDC-283
*/ */