diff --git a/src/Cache/PropertyResolver/AssociationResolver.php b/src/Cache/PropertyResolver/AssociationResolver.php index e59644a2..7034fb83 100644 --- a/src/Cache/PropertyResolver/AssociationResolver.php +++ b/src/Cache/PropertyResolver/AssociationResolver.php @@ -73,7 +73,9 @@ public function resolveSubscription( if (null !== $if = $routeMetadata->purgeOn->if) { $expression = (string) $if; - $if = new Expression(str_replace('obj', 'obj.'.$this->createGetter($associationClass, $associationTarget), $expression)); + $getter = $this->createGetter($associationClass, $associationTarget); + $inverseIf = str_replace('obj', 'obj.'.$this->createGetter($associationClass, $associationTarget), $expression); + $if = new Expression("obj.$getter !== null && ($inverseIf)"); } yield new PurgeSubscription( diff --git a/tests/Application/ConfigurationTest.php b/tests/Application/ConfigurationTest.php index 6d484922..82491300 100644 --- a/tests/Application/ConfigurationTest.php +++ b/tests/Application/ConfigurationTest.php @@ -243,6 +243,23 @@ public static function configurationWithoutTargetProvider(): iterable ], ], ]; + + /* @see PersonController::personCarsList */ + yield [ + 'entity' => Car::class, + 'subscription' => [ + 'routeName' => 'person_cars_list', + 'routeParams' => [ + 'id' => [ + 'type' => PropertyValues::type(), + 'values' => [ + Kernel::MAJOR_VERSION > 5 ? 'owner?.id' : 'owner.id', + ], + ], + ], + 'if' => "obj.owner !== null && (obj.owner.firstName === 'John')", + ], + ]; } public static function configurationWithTargetProvider(): iterable diff --git a/tests/Cache/PropertyResolver/AssociationResolverTestCase.php b/tests/Cache/PropertyResolver/AssociationResolverTestCase.php index adc8e93f..4534339e 100644 --- a/tests/Cache/PropertyResolver/AssociationResolverTestCase.php +++ b/tests/Cache/PropertyResolver/AssociationResolverTestCase.php @@ -104,7 +104,7 @@ classMetadata: $classMetadata, $subscription[0]->routeParams['param1'], ); self::assertEquals(new RawValues('const'), $subscription[0]->routeParams['param2']); - self::assertSame('obj.getFoo().isActive() === true', (string) $subscription[0]->if); + self::assertSame('obj.getFoo() !== null && (obj.getFoo().isActive() === true)', (string) $subscription[0]->if); } abstract public static function associationProvider(): iterable; diff --git a/tests/Functional/TestApplication/Controller/PersonController.php b/tests/Functional/TestApplication/Controller/PersonController.php index 30eac8e3..30d740c7 100644 --- a/tests/Functional/TestApplication/Controller/PersonController.php +++ b/tests/Functional/TestApplication/Controller/PersonController.php @@ -122,6 +122,10 @@ public function personListForCountryAction(?Country $country = null) #[Route('/{id}/cars', 'person_cars_list')] #[AnnotationRoute('/{id}/cars', name: 'person_cars_list')] #[PurgeOn(Person::class, target: 'cars')] + #[PurgeOn(Person::class, + target: 'cars', + if: "obj.firstName === 'John'", + )] public function personCarsList(Person $person) { }