Skip to content

Commit

Permalink
Add compatibility with doctrine/orm 3
Browse files Browse the repository at this point in the history
  • Loading branch information
franmomu committed Nov 19, 2023
1 parent 35531fd commit e4c9316
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
6 changes: 6 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ parameters:
# The class "AuditController" is deprecated and extends a class that is only supported in "symfony/http-foundation" < 5.
# NEXT_MAJOR: Remove these files
- src/Controller/AuditController.php
ignoreErrors:
# For compatibility with doctrine/orm 2 and 3
- '#.+ has invalid type Doctrine\\ORM\\Mapping\\AssociationMapping\.$#'
- '#^Property SimpleThings\\EntityAudit\\Collection\\AuditedCollection\:\:\$associationDefinition has unknown class Doctrine\\ORM\\Mapping\\AssociationMapping as its type\.$#'
- '#^Cannot access offset ''relationToSourceKey…'' on array\<string, mixed\>\|Doctrine\\ORM\\Mapping\\AssociationMapping\.$#'
- '#^Cannot access offset ''relationToTargetKey…'' on array\<string, mixed\>\|Doctrine\\ORM\\Mapping\\AssociationMapping\.$#'
9 changes: 8 additions & 1 deletion psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.11.1@e33492398bd4e5e2ab60e331d445979bd83feecd">
<files psalm-version="5.15.0@5c774aca4746caf3d239d9c8cadb9f882ca29352">
<!-- NEXT_MAJOR: Remove this file -->
<file src="src/Controller/AuditController.php">
<UndefinedClass occurrences="1">
<code>Controller</code>
</UndefinedClass>
</file>
<!-- For compatibility with doctrine/orm 2 and 3 -->
<file src="src/EventListener/LogRevisionsListener.php">
<UndefinedClass>
<code><![CDATA[array<string, mixed>|AssociationMapping]]></code>
<code><![CDATA[array<string, mixed>|AssociationMapping]]></code>
</UndefinedClass>
</file>
</files>
3 changes: 2 additions & 1 deletion src/Collection/AuditedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use SimpleThings\EntityAudit\AuditConfiguration;
use SimpleThings\EntityAudit\AuditReader;
Expand Down Expand Up @@ -70,7 +71,7 @@ public function __construct(
protected AuditReader $auditReader,
protected $class,
protected ClassMetadata $metadata,
protected $associationDefinition,
protected array|AssociationMapping $associationDefinition,
protected array $foreignKeys,
protected $revision
) {
Expand Down
33 changes: 22 additions & 11 deletions src/EventListener/LogRevisionsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Doctrine\ORM\Event\PostPersistEventArgs;
use Doctrine\ORM\Event\PostUpdateEventArgs;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\AssociationMapping;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Persisters\Entity\EntityPersister;
use Doctrine\ORM\UnitOfWork;
Expand Down Expand Up @@ -451,12 +452,15 @@ private function getInsertRevisionSQL(EntityManagerInterface $em, ClassMetadata
}

/**
* @param ClassMetadata<object> $class
* @param ClassMetadata<object> $targetClass
* @param array<string, mixed> $assoc
* @param ClassMetadata<object> $class
* @param ClassMetadata<object> $targetClass
* @param array<string, mixed>|AssociationMapping $assoc
*/
private function getInsertJoinTableRevisionSQL(ClassMetadata $class, ClassMetadata $targetClass, array $assoc): string
{
private function getInsertJoinTableRevisionSQL(
ClassMetadata $class,
ClassMetadata $targetClass,
array|AssociationMapping $assoc
): string {
$cacheKey = $class->name.'.'.$targetClass->name;
if (!isset($this->insertJoinTableRevisionSQL[$cacheKey])
&& isset($assoc['relationToSourceKeyColumns'], $assoc['relationToTargetKeyColumns'], $assoc['joinTable']['name'])) {
Expand Down Expand Up @@ -608,13 +612,20 @@ private function saveRevisionEntityData(EntityManagerInterface $em, ClassMetadat
}

/**
* @param array<string, mixed> $assoc
* @param array<string, mixed> $entityData
* @param ClassMetadata<object> $class
* @param ClassMetadata<object> $targetClass
* @param array<string, mixed>|AssociationMapping $assoc
* @param array<string, mixed> $entityData
* @param ClassMetadata<object> $class
* @param ClassMetadata<object> $targetClass
*/
private function recordRevisionForManyToManyEntity(object $relatedEntity, EntityManagerInterface $em, string $revType, array $entityData, array $assoc, ClassMetadata $class, ClassMetadata $targetClass): void
{
private function recordRevisionForManyToManyEntity(
object $relatedEntity,
EntityManagerInterface $em,
string $revType,
array $entityData,
array|AssociationMapping $assoc,
ClassMetadata $class,
ClassMetadata $targetClass
): void {
$conn = $em->getConnection();
$joinTableParams = [$this->getRevisionId($conn), $revType];
$joinTableTypes = [\PDO::PARAM_INT, \PDO::PARAM_STR];
Expand Down

0 comments on commit e4c9316

Please sign in to comment.