Skip to content

Commit b5c3b56

Browse files
authored
Merge pull request #12563 from garak/defaultStringSchemaLength
Make default string length configurable in SchemaTool
2 parents 5c56fe8 + 4cd36a9 commit b5c3b56

4 files changed

Lines changed: 88 additions & 2 deletions

File tree

docs/en/reference/advanced-configuration.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,24 @@ For development you should use an array cache like
232232
``Symfony\Component\Cache\Adapter\ArrayAdapter``
233233
which only caches data on a per-request basis.
234234

235+
Default String Type Schema Length (**OPTIONAL**)
236+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237+
238+
.. code-block:: php
239+
240+
<?php
241+
$config->setDefaultStringTypeSchemaLength(191);
242+
$config->getDefaultStringTypeSchemaLength();
243+
244+
Gets or sets the default length used by the SchemaTool for string columns that
245+
have no explicit ``length`` value in the mapping. This is used when the schema is
246+
created from metadata and when discriminator columns are generated for single-table
247+
inheritance.
248+
249+
The default value is ``255``. Setting it explicitly is useful for platforms or
250+
storage layouts that require shorter default string lengths, for example MySQL
251+
``utf8mb4`` schemas that often use ``191`` for indexed varchar columns.
252+
235253
Query Cache (**RECOMMENDED**)
236254
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237255

src/Configuration.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,4 +723,14 @@ public function getEagerFetchBatchSize(): int
723723
{
724724
return $this->attributes['fetchModeSubselectBatchSize'] ?? 100;
725725
}
726+
727+
public function setDefaultStringTypeSchemaLength(int $length): void
728+
{
729+
$this->attributes['defaultStringTypeSchemaLength'] = $length;
730+
}
731+
732+
public function getDefaultStringTypeSchemaLength(): int
733+
{
734+
return $this->attributes['defaultStringTypeSchemaLength'] ?? 255;
735+
}
726736
}

src/Tools/SchemaTool.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ private function addDiscriminatorColumnDefinition(ClassMetadata $class, Table $t
567567

568568
if (strtolower($discrColumn->type) === 'string' && ! isset($discrColumn->length)) {
569569
$discrColumn->type = 'string';
570-
$discrColumn->length = 255;
570+
$discrColumn->length = $this->em->getConfiguration()->getDefaultStringTypeSchemaLength();
571571
}
572572

573573
$options = [
@@ -623,7 +623,7 @@ private function gatherColumn(
623623
$options['platformOptions']['version'] = $class->isVersioned && $class->versionField === $mapping->fieldName;
624624

625625
if (strtolower($columnType) === 'string' && $options['length'] === null) {
626-
$options['length'] = 255;
626+
$options['length'] = $this->em->getConfiguration()->getDefaultStringTypeSchemaLength();
627627
}
628628

629629
if (isset($mapping->precision)) {

tests/Tests/ORM/Tools/SchemaToolTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,64 @@ public function testSetDiscriminatorColumnWithoutLength(): void
278278
self::assertEquals(255, $column->getLength());
279279
}
280280

281+
public function testSetDefaultStringLength(): void
282+
{
283+
$em = $this->getTestEntityManager();
284+
$em->getConfiguration()->setDefaultStringTypeSchemaLength(191);
285+
$schemaTool = new SchemaTool($em);
286+
$metadata = $em->getClassMetadata(FirstEntity::class);
287+
288+
$schema = $schemaTool->getSchemaFromMetadata([$metadata]);
289+
290+
self::assertTrue($schema->hasTable('first_entity'));
291+
$table = $schema->getTable('first_entity');
292+
293+
self::assertTrue($table->hasColumn('name'));
294+
$column = $table->getColumn('name');
295+
296+
self::assertEquals(191, $column->getLength());
297+
}
298+
299+
public function testSetDefaultStringLengthDoesNotOverrideExplicitLength(): void
300+
{
301+
$em = $this->getTestEntityManager();
302+
$em->getConfiguration()->setDefaultStringTypeSchemaLength(191);
303+
$schemaTool = new SchemaTool($em);
304+
$metadata = $em->getClassMetadata(FirstEntity::class);
305+
$metadata->fieldMappings['name']->length = 32;
306+
307+
$schema = $schemaTool->getSchemaFromMetadata([$metadata]);
308+
309+
self::assertTrue($schema->hasTable('first_entity'));
310+
$table = $schema->getTable('first_entity');
311+
312+
self::assertTrue($table->hasColumn('name'));
313+
$column = $table->getColumn('name');
314+
315+
self::assertEquals(32, $column->getLength());
316+
}
317+
318+
public function testSetDiscriminatorColumnWithCustomDefaultLength(): void
319+
{
320+
$em = $this->getTestEntityManager();
321+
$em->getConfiguration()->setDefaultStringTypeSchemaLength(191);
322+
$schemaTool = new SchemaTool($em);
323+
$metadata = $em->getClassMetadata(FirstEntity::class);
324+
325+
$metadata->setInheritanceType(ClassMetadata::INHERITANCE_TYPE_SINGLE_TABLE);
326+
$metadata->setDiscriminatorColumn(['name' => 'discriminator', 'type' => 'string']);
327+
328+
$schema = $schemaTool->getSchemaFromMetadata([$metadata]);
329+
330+
self::assertTrue($schema->hasTable('first_entity'));
331+
$table = $schema->getTable('first_entity');
332+
333+
self::assertTrue($table->hasColumn('discriminator'));
334+
$column = $table->getColumn('discriminator');
335+
336+
self::assertEquals(191, $column->getLength());
337+
}
338+
281339
public function testSetDiscriminatorColumnWithEnumType(): void
282340
{
283341
if (! class_exists(EnumType::class)) {

0 commit comments

Comments
 (0)