Skip to content
Open
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
36 changes: 19 additions & 17 deletions src/Tools/SchemaTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,18 @@ class SchemaTool
private readonly AbstractPlatform $platform;
private readonly QuoteStrategy $quoteStrategy;
private readonly AbstractSchemaManager $schemaManager;
private readonly bool $useDbalEditorApi;

/**
* Initializes a new SchemaTool instance that uses the connection of the
* provided EntityManager.
*/
public function __construct(private readonly EntityManagerInterface $em)
{
$this->platform = $em->getConnection()->getDatabasePlatform();
$this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
$this->schemaManager = $em->getConnection()->createSchemaManager();
$this->platform = $em->getConnection()->getDatabasePlatform();
$this->quoteStrategy = $em->getConfiguration()->getQuoteStrategy();
$this->schemaManager = $em->getConnection()->createSchemaManager();
$this->useDbalEditorApi = false;

@greg0ire greg0ire Aug 7, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The follow up will make this dynamic, allowing to revert the drop in code coverage.

}

/**
Expand Down Expand Up @@ -210,8 +212,8 @@ public function getSchemaFromMetadata(array $classes): Schema

$tableName = $this->quoteStrategy->getTableName($class, $this->platform);

// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
if (method_exists(Schema::class, 'edit')) {
/** @phpstan-ignore if.alwaysFalse (might become true very soon) */
if ($this->useDbalEditorApi) {
$table = new Table(
name: $tableName,
configuration: $metadataSchemaConfig->toTableConfiguration(),
Expand Down Expand Up @@ -298,8 +300,8 @@ public function getSchemaFromMetadata(array $classes): Schema
$this->platform,
);
// TODO: This seems rather hackish, can we optimize it?
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API for version detection)
if (method_exists(Schema::class, 'edit')) {
/** @phpstan-ignore if.alwaysFalse (might become true very soon) */
if ($this->useDbalEditorApi) {
// New API: modify column using table editor (creates new table object)
// This is safe because we'll add the table to schema later after all modifications
$table = $table->edit()->modifyColumnByUnquotedName(
Expand Down Expand Up @@ -436,8 +438,8 @@ public function getSchemaFromMetadata(array $classes): Schema
}

if (isset($class->table['options'])) {
/** @phpstan-ignore function.impossibleType (method existence depends on DBAL version) */
if (method_exists(Schema::class, 'edit')) {
/** @phpstan-ignore if.alwaysFalse (might become true very soon) */
if ($this->useDbalEditorApi) {
$table = $table->edit()->setOptions($class->table['options'])->create();
} else {
foreach ($class->table['options'] as $key => $val) {
Expand All @@ -449,8 +451,8 @@ public function getSchemaFromMetadata(array $classes): Schema
$processedClasses[$class->name] = true;

// Add the fully populated table to the schema
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
if (method_exists(Schema::class, 'edit')) {
/** @phpstan-ignore if.alwaysFalse (might become true very soon) */
if ($this->useDbalEditorApi) {
// @phpstan-ignore method.notFound (Using unreleased Schema::edit() API)
$schemaEditor = $schema->edit();
$schemaEditor->addTable($table);
Expand Down Expand Up @@ -508,8 +510,8 @@ public function getSchemaFromMetadata(array $classes): Schema
continue;
}

// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
if (method_exists(Schema::class, 'edit')) {
/** @phpstan-ignore if.alwaysFalse (might become true very soon) */
if ($this->useDbalEditorApi) {
// the table might have been dropped by a listener, so we ignore this error
if ($schema->hasTable($fkData['table']->getObjectName()->toString())) {
$schema = $schema->edit()->modifyTable(
Expand Down Expand Up @@ -778,8 +780,8 @@ private function gatherRelationsSql(
$tableName = $this->quoteStrategy->getJoinTableName($mapping, $foreignClass, $this->platform);

// Create the join table object
// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
if (method_exists(Schema::class, 'edit')) {
/** @phpstan-ignore if.alwaysFalse (might become true very soon) */
if ($this->useDbalEditorApi) {
$theJoinTable = new Table(
name: $tableName,
options: $joinTable->options,
Expand Down Expand Up @@ -822,8 +824,8 @@ private function gatherRelationsSql(

self::addPrimaryKeyConstraint($theJoinTable, $primaryKeyColumns);

// @phpstan-ignore function.impossibleType (Using unreleased Schema::edit() API)
if (method_exists(Schema::class, 'edit')) {
/** @phpstan-ignore if.alwaysFalse (might become true very soon) */
if ($this->useDbalEditorApi) {
$joinTablesToAdd[] = $theJoinTable;
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Tests/ORM/Functional/Ticket/GH11501Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function testDeleteOneToManyCollectionWithSingleTableInheritance(): void
}

#[ORM\Entity]
#[ORM\Table(name: 'one_to_many_sti_test_entities_parent_join')]
#[ORM\Table(name: 'one_to_many_single_table_inheritance_test_entities_parent_join')]
#[ORM\InheritanceType('SINGLE_TABLE')]
#[ORM\DiscriminatorColumn(name: 'type', type: 'string')]
#[ORM\DiscriminatorMap([
Expand Down
Loading