diff --git a/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/Category.php b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/Category.php new file mode 100644 index 00000000000..f80d89bc98c --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/Category.php @@ -0,0 +1,43 @@ +name = $name; + $this->type = $type; + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/CategoryTypeSQLFilter.php b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/CategoryTypeSQLFilter.php new file mode 100644 index 00000000000..459e0c2f2f7 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/CategoryTypeSQLFilter.php @@ -0,0 +1,22 @@ +getName() === Category::class) { + return sprintf('%s.%s = %s', $targetTableAlias, $targetEntity->fieldMappings['type']['fieldName'], $this->getParameter('type')); + } + + return ''; + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php new file mode 100644 index 00000000000..14a44ffe4c9 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/ChangeFiltersTest.php @@ -0,0 +1,56 @@ +setUpEntitySchema([ + Company::class, + Category::class, + ]); + } + + private function prepareData(): void + { + $cat1 = new Category('cat1', self::CAT_FOO); + $cat2 = new Category('cat2', self::CAT_BAR); + $companyA = new Company(self::COMPANY_A, [$cat1, $cat2]); + + $this->_em->persist($cat1); + $this->_em->persist($cat2); + $this->_em->persist($companyA); + $this->_em->flush(); + $this->_em->clear(); + } + + public function testIndexAliasUpdatedWithUpdatedFilter(): void + { + $this->prepareData(); + + $company = $this->_em->getRepository(Company::class)->findOneBy([]); + + self::assertCount(2, $company->categories); + self::assertEquals([self::CAT_FOO, self::CAT_BAR], $company->categories->map(function (Category $c): string { return $c->type; })->getValues()); + + $this->_em->clear(); + $this->_em->getConfiguration()->addFilter(CategoryTypeSQLFilter::class, CategoryTypeSQLFilter::class); + $this->_em->getFilters()->enable(CategoryTypeSQLFilter::class)->setParameter('type', self::CAT_FOO); + + $company = $this->_em->getRepository(Company::class)->findOneBy([]); + + self::assertCount(1, $company->categories); + self::assertEquals([self::CAT_FOO], $company->categories->map(function (Category $c): string { return $c->type; })->getValues()); + } +} diff --git a/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/Company.php b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/Company.php new file mode 100644 index 00000000000..4490b303d98 --- /dev/null +++ b/tests/Tests/ORM/Functional/Ticket/SwitchContextWithFilterAndIndexedRelation/Company.php @@ -0,0 +1,46 @@ + + */ + public $categories; + + /** @param Category[] $categories */ + public function __construct(string $name, array $categories) + { + $this->name = $name; + $this->categories = new ArrayCollection($categories); + } +}