28
28
use Doctrine \ORM \EntityManagerInterface ;
29
29
use Doctrine \ORM \Event \OnFlushEventArgs ;
30
30
use Doctrine \ORM \Event \PreUpdateEventArgs ;
31
+ use Doctrine \ORM \Mapping \AssociationMapping ;
31
32
use Doctrine \ORM \PersistentCollection ;
32
33
use Symfony \Component \PropertyAccess \PropertyAccess ;
33
34
use Symfony \Component \PropertyAccess \PropertyAccessorInterface ;
@@ -57,6 +58,7 @@ public function preUpdate(PreUpdateEventArgs $eventArgs): void
57
58
$ this ->gatherResourceAndItemTags ($ object , true );
58
59
59
60
$ changeSet = $ eventArgs ->getEntityChangeSet ();
61
+ // @phpstan-ignore-next-line
60
62
$ objectManager = method_exists ($ eventArgs , 'getObjectManager ' ) ? $ eventArgs ->getObjectManager () : $ eventArgs ->getEntityManager ();
61
63
$ associationMappings = $ objectManager ->getClassMetadata (ClassUtils::getClass ($ eventArgs ->getObject ()))->getAssociationMappings ();
62
64
@@ -75,6 +77,7 @@ public function preUpdate(PreUpdateEventArgs $eventArgs): void
75
77
*/
76
78
public function onFlush (OnFlushEventArgs $ eventArgs ): void
77
79
{
80
+ // @phpstan-ignore-next-line
78
81
$ em = method_exists ($ eventArgs , 'getObjectManager ' ) ? $ eventArgs ->getObjectManager () : $ eventArgs ->getEntityManager ();
79
82
$ uow = $ em ->getUnitOfWork ();
80
83
@@ -125,12 +128,19 @@ private function gatherResourceAndItemTags(object $entity, bool $purgeItem): voi
125
128
private function gatherRelationTags (EntityManagerInterface $ em , object $ entity ): void
126
129
{
127
130
$ associationMappings = $ em ->getClassMetadata (ClassUtils::getClass ($ entity ))->getAssociationMappings ();
128
- foreach (array_keys ($ associationMappings ) as $ property ) {
131
+ /** @var array|AssociationMapping $associationMapping according to the version of doctrine orm */
132
+ foreach ($ associationMappings as $ property => $ associationMapping ) {
133
+ if ($ associationMapping instanceof AssociationMapping && ($ associationMapping ->targetEntity ?? null ) && !$ this ->resourceClassResolver ->isResourceClass ($ associationMapping ->targetEntity )) {
134
+ return ;
135
+ }
136
+
129
137
if (
130
- \array_key_exists ('targetEntity ' , $ associationMappings [$ property ])
131
- && !$ this ->resourceClassResolver ->isResourceClass ($ associationMappings [$ property ]['targetEntity ' ])) {
138
+ \is_array ($ associationMapping )
139
+ && \array_key_exists ('targetEntity ' , $ associationMapping )
140
+ && !$ this ->resourceClassResolver ->isResourceClass ($ associationMapping ['targetEntity ' ])) {
132
141
return ;
133
142
}
143
+
134
144
if ($ this ->propertyAccessor ->isReadable ($ entity , $ property )) {
135
145
$ this ->addTagsFor ($ this ->propertyAccessor ->getValue ($ entity , $ property ));
136
146
}
0 commit comments