diff --git a/composer.json b/composer.json index fbcbe56f..187c9a0e 100644 --- a/composer.json +++ b/composer.json @@ -11,7 +11,7 @@ "require": { "php": "^8.1", "doctrine/collections": "^1.8 || ^2.0", - "doctrine/dbal": "^3.6", + "doctrine/dbal": "^3.6 || ^4.0", "doctrine/event-manager": "^1.2 || ^2.0", "doctrine/orm": "^2.14 || ^3.0", "doctrine/persistence": "^3.0", diff --git a/src/AuditReader.php b/src/AuditReader.php index 482e1018..bd3ca09f 100644 --- a/src/AuditReader.php +++ b/src/AuditReader.php @@ -292,7 +292,7 @@ public function find($className, $id, $revision, array $options = []) $allDiscrValues = array_flip($classMetadata->discriminatorMap); $queriedDiscrValues = [$this->em->getConnection()->quote($classMetadata->discriminatorValue)]; foreach ($classMetadata->subClasses as $subclassName) { - $queriedDiscrValues[] = $this->em->getConnection()->quote($allDiscrValues[$subclassName]); + $queriedDiscrValues[] = $this->em->getConnection()->quote(strval($allDiscrValues[$subclassName])); } $whereSQL .= \sprintf( diff --git a/src/EventListener/CreateSchemaListener.php b/src/EventListener/CreateSchemaListener.php index c76329a3..b2fe8819 100644 --- a/src/EventListener/CreateSchemaListener.php +++ b/src/EventListener/CreateSchemaListener.php @@ -144,8 +144,25 @@ private function createForeignKeys(Table $relatedTable, Table $revisionsTable): $primaryKey = $revisionsTable->getPrimaryKey(); \assert(null !== $primaryKey); + /** + * doctrine/dbal 3 support -- Table::addForeignKeyConstraint() takes a Table instead of a string + * + * NEXT_MAJOR: remove this `if` block + */ + if (version_compare(\Composer\InstalledVersions::getVersion('doctrine/dbal') ?? '', '4.0.0', '<')) { + $relatedTable->addForeignKeyConstraint( + $revisionsTable, // @phpstan-ignore-line doctrine/dbal 3 support for old addForeignKeyConstraint() signature + [$this->config->getRevisionFieldName()], + $primaryKey->getColumns(), + [], + $revisionForeignKeyName + ); + + return; + } + $relatedTable->addForeignKeyConstraint( - $revisionsTable, + $revisionsTable->getName(), [$this->config->getRevisionFieldName()], $primaryKey->getColumns(), [], diff --git a/src/EventListener/LogRevisionsListener.php b/src/EventListener/LogRevisionsListener.php index 4ed98432..67a4b0ed 100644 --- a/src/EventListener/LogRevisionsListener.php +++ b/src/EventListener/LogRevisionsListener.php @@ -16,6 +16,7 @@ use Doctrine\Common\EventSubscriber; use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception; +use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; use Doctrine\ORM\EntityManagerInterface; @@ -380,8 +381,16 @@ private function getRevisionId(Connection $conn) ); $revisionId = $conn->lastInsertId(); - if (false === $revisionId) { - throw new \RuntimeException('Unable to retrieve the last revision id.'); + /** + * Preceding lastInsertId throws Doctrine\DBAL\Exception\DriverException on doctrine/dbal 4+, making the + * next check unnecessary. + * + * NEXT_MAJOR: Remove the following block + */ + if (version_compare(\Composer\InstalledVersions::getVersion('doctrine/dbal') ?? '', '4.0.0', '<')) { + if (false === $revisionId) { // @phpstan-ignore-line doctrine/dbal 3 lastInsertId() can return false + throw new \RuntimeException('Unable to retrieve the last revision id.'); + } } $this->revisionId = $revisionId; @@ -520,7 +529,7 @@ private function saveRevisionEntityData(EntityManagerInterface $em, ClassMetadat $conn = $em->getConnection(); $params = [$this->getRevisionId($conn), $revType]; - $types = [\PDO::PARAM_INT, \PDO::PARAM_STR]; + $types = [ParameterType::INTEGER, ParameterType::STRING]; $fields = []; @@ -544,7 +553,7 @@ private function saveRevisionEntityData(EntityManagerInterface $em, ClassMetadat $fields[$sourceColumn] = true; if (null === $data) { $params[] = null; - $types[] = \PDO::PARAM_STR; + $types[] = ParameterType::STRING; } else { $params[] = $relatedId[$targetClass->fieldNames[$targetColumn]] ?? null; $types[] = $targetClass->getTypeOfField($targetClass->getFieldForColumn($targetColumn)); @@ -639,7 +648,7 @@ private function recordRevisionForManyToManyEntity( ): void { $conn = $em->getConnection(); $joinTableParams = [$this->getRevisionId($conn), $revType]; - $joinTableTypes = [\PDO::PARAM_INT, \PDO::PARAM_STR]; + $joinTableTypes = [ParameterType::INTEGER, ParameterType::STRING]; foreach (self::getRelationToSourceKeyColumns($assoc) as $targetColumn) { $joinTableParams[] = $entityData[$class->fieldNames[$targetColumn]]; diff --git a/tests/CoreTest.php b/tests/CoreTest.php index b6078d31..22c419cb 100644 --- a/tests/CoreTest.php +++ b/tests/CoreTest.php @@ -14,7 +14,7 @@ namespace Sonata\EntityAuditBundle\Tests; use Doctrine\DBAL\Exception\DriverException; -use Doctrine\DBAL\Platforms\SqlitePlatform; +use Doctrine\DBAL\Platforms\SQLitePlatform; use SimpleThings\EntityAudit\ChangedEntity; use SimpleThings\EntityAudit\Exception\NoRevisionFoundException; use SimpleThings\EntityAudit\Exception\NotAuditedException; @@ -485,7 +485,7 @@ public function testRevisionForeignKeys(): void { $em = $this->getEntityManager(); - $isSqlitePlatform = $em->getConnection()->getDatabasePlatform() instanceof SqlitePlatform; + $isSqlitePlatform = $em->getConnection()->getDatabasePlatform() instanceof SQLitePlatform; $updateForeignKeysConfig = false; if ($isSqlitePlatform) { diff --git a/tests/Types/ConvertToPHPType.php b/tests/Types/ConvertToPHPType.php index 5dc68f1b..af810241 100644 --- a/tests/Types/ConvertToPHPType.php +++ b/tests/Types/ConvertToPHPType.php @@ -28,12 +28,12 @@ public function canRequireSQLConversion(): bool return true; } - public function convertToPHPValueSQL($sqlExpr, $platform): string + public function convertToPHPValueSQL(string $sqlExpr, AbstractPlatform $platform): string { return \sprintf('UPPER(%s)', $sqlExpr); } - public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform): string + public function convertToDatabaseValueSQL(string $sqlExpr, AbstractPlatform $platform): string { return \sprintf('LOWER(%s)', $sqlExpr); } diff --git a/tests/Types/Issue196Type.php b/tests/Types/Issue196Type.php index 9797569f..ad72e9ae 100644 --- a/tests/Types/Issue196Type.php +++ b/tests/Types/Issue196Type.php @@ -31,7 +31,7 @@ public function canRequireSQLConversion(): bool return true; } - public function convertToDatabaseValueSQL($sqlExpr, AbstractPlatform $platform): string + public function convertToDatabaseValueSQL(string $sqlExpr, AbstractPlatform $platform): string { return \sprintf('lower(%s)', $sqlExpr); }