Skip to content

Commit f8a298b

Browse files
committed
use Table::addPrimaryKeyConstraint() with Doctrine DBAL 4.3+
1 parent 9431a37 commit f8a298b

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

SchemaListener/AbstractSchemaListener.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\Exception\TableNotFoundException;
16+
use Doctrine\DBAL\Schema\Name\Identifier;
17+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
18+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1619
use Doctrine\DBAL\Schema\Table;
1720
use Doctrine\DBAL\Types\Types;
1821
use Doctrine\ORM\Tools\Event\GenerateSchemaEventArgs;
@@ -30,7 +33,12 @@ protected function getIsSameDatabaseChecker(Connection $connection): \Closure
3033
$table->addColumn('id', Types::INTEGER)
3134
->setAutoincrement(true)
3235
->setNotnull(true);
33-
$table->setPrimaryKey(['id']);
36+
37+
if (class_exists(PrimaryKeyConstraint::class)) {
38+
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('id'))], true));
39+
} else {
40+
$table->setPrimaryKey(['id']);
41+
}
3442

3543
$schemaManager->createTable($table);
3644

Security/RememberMe/DoctrineTokenProvider.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
use Doctrine\DBAL\Connection;
1515
use Doctrine\DBAL\ParameterType;
16+
use Doctrine\DBAL\Schema\Name\Identifier;
17+
use Doctrine\DBAL\Schema\Name\UnqualifiedName;
18+
use Doctrine\DBAL\Schema\PrimaryKeyConstraint;
1619
use Doctrine\DBAL\Schema\Schema;
1720
use Doctrine\DBAL\Types\Types;
1821
use Symfony\Component\Security\Core\Authentication\RememberMe\PersistentToken;
@@ -193,6 +196,11 @@ private function addTableToSchema(Schema $schema): void
193196
$table->addColumn('lastUsed', Types::DATETIME_IMMUTABLE);
194197
$table->addColumn('class', Types::STRING, ['length' => 100]);
195198
$table->addColumn('username', Types::STRING, ['length' => 200]);
196-
$table->setPrimaryKey(['series']);
199+
200+
if (class_exists(PrimaryKeyConstraint::class)) {
201+
$table->addPrimaryKeyConstraint(new PrimaryKeyConstraint(null, [new UnqualifiedName(Identifier::unquoted('series'))], true));
202+
} else {
203+
$table->setPrimaryKey(['series']);
204+
}
197205
}
198206
}

0 commit comments

Comments
 (0)