From bad44820fd9bd2f0510b2e629fe226aff7c87579 Mon Sep 17 00:00:00 2001 From: "David W. Lloyd" Date: Sat, 27 Aug 2022 11:47:53 -0400 Subject: [PATCH 1/6] Update ModelManager to support Doctrine\ORM\Query directly Makes life easier when you want to set sorting, caching, etc. directly from an admin, e.g. in configureFormFields --- src/Model/ModelManager.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Model/ModelManager.php b/src/Model/ModelManager.php index 4dc71a571..ee21d8fb6 100644 --- a/src/Model/ModelManager.php +++ b/src/Model/ModelManager.php @@ -21,6 +21,7 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\ORM\UnitOfWork; @@ -208,7 +209,7 @@ public function createQuery(string $class, string $alias = 'o'): BaseProxyQueryI public function supportsQuery(object $query): bool { - return $query instanceof ProxyQuery || $query instanceof QueryBuilder; + return $query instanceof ProxyQuery || $query instanceof Query || $query instanceof QueryBuilder; } public function executeQuery(object $query) @@ -217,7 +218,7 @@ public function executeQuery(object $query) return $query->getQuery()->execute(); } - if ($query instanceof ProxyQuery) { + if ($query instanceof Query || $query instanceof ProxyQuery) { /** @phpstan-var Paginator $results */ $results = $query->execute(); @@ -225,10 +226,11 @@ public function executeQuery(object $query) } throw new \InvalidArgumentException(sprintf( - 'Argument 1 passed to %s() must be an instance of %s or %s', + 'Argument 1 passed to %s() must be an instance of %s, %s, or %s', __METHOD__, QueryBuilder::class, - ProxyQuery::class, + Query::class, + ProxyQuery::class )); } From 3b65e4209b928956070d77af4ded8d3efc895489 Mon Sep 17 00:00:00 2001 From: "David W. Lloyd" Date: Sat, 27 Aug 2022 15:07:00 -0400 Subject: [PATCH 2/6] Update src/Model/ModelManager.php Co-authored-by: Vincent Langlet --- src/Model/ModelManager.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Model/ModelManager.php b/src/Model/ModelManager.php index ee21d8fb6..a4791c53a 100644 --- a/src/Model/ModelManager.php +++ b/src/Model/ModelManager.php @@ -218,7 +218,11 @@ public function executeQuery(object $query) return $query->getQuery()->execute(); } - if ($query instanceof Query || $query instanceof ProxyQuery) { + if ($query instanceof Query) { + return $query->execute(); + } + + if ($query instanceof ProxyQuery) { /** @phpstan-var Paginator $results */ $results = $query->execute(); From b8fead9f1120dd5a78f4fb1a537bcd359776f052 Mon Sep 17 00:00:00 2001 From: "David W. Lloyd" Date: Sat, 27 Aug 2022 17:40:24 -0400 Subject: [PATCH 3/6] Update ModelManagerTest.php Added test for Query class --- tests/Model/ModelManagerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Model/ModelManagerTest.php b/tests/Model/ModelManagerTest.php index 075adb822..a50ae9ef7 100644 --- a/tests/Model/ModelManagerTest.php +++ b/tests/Model/ModelManagerTest.php @@ -149,6 +149,7 @@ public function testSupportsQuery(bool $expected, object $object): void public function supportsQueryDataProvider(): iterable { yield [true, new ProxyQuery($this->createMock(QueryBuilder::class))]; + yield [true, $this->createMock(Query::class)]; yield [true, $this->createMock(QueryBuilder::class)]; yield [false, new \stdClass()]; } From 66ff82a9f1bc57206626c3807968867a2552600f Mon Sep 17 00:00:00 2001 From: "David W. Lloyd" Date: Sun, 28 Aug 2022 10:17:13 -0400 Subject: [PATCH 4/6] Update ModelManagerTest.php Forgot use statement for Query --- tests/Model/ModelManagerTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Model/ModelManagerTest.php b/tests/Model/ModelManagerTest.php index a50ae9ef7..6cc1acac7 100644 --- a/tests/Model/ModelManagerTest.php +++ b/tests/Model/ModelManagerTest.php @@ -20,6 +20,7 @@ use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\OptimisticLockException; +use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use PHPUnit\Framework\MockObject\MockObject; From b55ae7be3cf3d4d2e80db654c2815d71c52566da Mon Sep 17 00:00:00 2001 From: "David W. Lloyd" Date: Sun, 28 Aug 2022 11:51:22 -0400 Subject: [PATCH 5/6] Update ModelManagerTest.php --- tests/Model/ModelManagerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Model/ModelManagerTest.php b/tests/Model/ModelManagerTest.php index 6cc1acac7..009a26030 100644 --- a/tests/Model/ModelManagerTest.php +++ b/tests/Model/ModelManagerTest.php @@ -17,10 +17,10 @@ use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; +use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\OptimisticLockException; -use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\Persistence\ManagerRegistry; use PHPUnit\Framework\MockObject\MockObject; @@ -150,7 +150,7 @@ public function testSupportsQuery(bool $expected, object $object): void public function supportsQueryDataProvider(): iterable { yield [true, new ProxyQuery($this->createMock(QueryBuilder::class))]; - yield [true, $this->createMock(Query::class)]; + yield [true, $this->createMock(AbstractQuery::class)]; yield [true, $this->createMock(QueryBuilder::class)]; yield [false, new \stdClass()]; } From 0681162c6a9fab6449177537dcefa5e5f92d7a5e Mon Sep 17 00:00:00 2001 From: "David W. Lloyd" Date: Sun, 28 Aug 2022 11:53:29 -0400 Subject: [PATCH 6/6] Update ModelManager.php Switch to AbstractQuery since that's where execute() lives, this way we can support NativeQuery, AND AbstractQuery can be mocked for tests. --- src/Model/ModelManager.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Model/ModelManager.php b/src/Model/ModelManager.php index a4791c53a..471ab283e 100644 --- a/src/Model/ModelManager.php +++ b/src/Model/ModelManager.php @@ -18,10 +18,10 @@ use Doctrine\DBAL\LockMode; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Types\Type; +use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Mapping\ClassMetadata; use Doctrine\ORM\OptimisticLockException; -use Doctrine\ORM\Query; use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\Tools\Pagination\Paginator; use Doctrine\ORM\UnitOfWork; @@ -209,7 +209,7 @@ public function createQuery(string $class, string $alias = 'o'): BaseProxyQueryI public function supportsQuery(object $query): bool { - return $query instanceof ProxyQuery || $query instanceof Query || $query instanceof QueryBuilder; + return $query instanceof ProxyQuery || $query instanceof AbstractQuery || $query instanceof QueryBuilder; } public function executeQuery(object $query) @@ -218,7 +218,7 @@ public function executeQuery(object $query) return $query->getQuery()->execute(); } - if ($query instanceof Query) { + if ($query instanceof AbstractQuery) { return $query->execute(); } @@ -233,7 +233,7 @@ public function executeQuery(object $query) 'Argument 1 passed to %s() must be an instance of %s, %s, or %s', __METHOD__, QueryBuilder::class, - Query::class, + AbstractQuery::class, ProxyQuery::class )); }