From e64c8979478ea8675f1a63927832f28efba2406a Mon Sep 17 00:00:00 2001 From: Claus Due Date: Fri, 25 Oct 2024 15:51:12 +0200 Subject: [PATCH] [BUGFIX] Correct doctrine proxy when used with non-SELECT statements (#2189) --- Classes/Service/RecordService.php | 4 ++-- Classes/Utility/DoctrineQueryProxy.php | 17 +++++++++++------ Tests/Unit/Service/RecordServiceTest.php | 11 +++++++---- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/Classes/Service/RecordService.php b/Classes/Service/RecordService.php index c549b9996..b65432f77 100644 --- a/Classes/Service/RecordService.php +++ b/Classes/Service/RecordService.php @@ -86,7 +86,7 @@ public function update(string $table, array $record) foreach ($record as $name => $value) { $builder->set($name, $value); } - return DoctrineQueryProxy::executeQueryOnQueryBuilder($builder); + return DoctrineQueryProxy::executeStatementOnQueryBuilder($builder); } /** @@ -98,7 +98,7 @@ public function delete(string $table, $recordOrUid): bool $queryBuilder = $this->getQueryBuilder($table); $queryBuilder->delete($table) ->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($clauseUid))); - return (bool) DoctrineQueryProxy::executeQueryOnQueryBuilder($queryBuilder); + return (bool) DoctrineQueryProxy::executeStatementOnQueryBuilder($queryBuilder); } public function preparedGet(string $table, string $fields, string $condition, array $values = []): array diff --git a/Classes/Utility/DoctrineQueryProxy.php b/Classes/Utility/DoctrineQueryProxy.php index 1f6627bf2..61513b243 100644 --- a/Classes/Utility/DoctrineQueryProxy.php +++ b/Classes/Utility/DoctrineQueryProxy.php @@ -7,12 +7,17 @@ class DoctrineQueryProxy { - /** - * Returns \Doctrine\DBAL\Result on v11+, \Doctrine\DBAL\Driver\ResultStatement on v10 - * - * @return Result - */ - public static function executeQueryOnQueryBuilder(QueryBuilder $queryBuilder) + public static function executeStatementOnQueryBuilder(QueryBuilder $queryBuilder): int + { + if (method_exists($queryBuilder, 'executeStatement')) { + return $queryBuilder->executeStatement(); + } + /** @var int $result */ + $result = $queryBuilder->execute(); + return $result; + } + + public static function executeQueryOnQueryBuilder(QueryBuilder $queryBuilder): Result { if (method_exists($queryBuilder, 'executeQuery')) { return $queryBuilder->executeQuery(); diff --git a/Tests/Unit/Service/RecordServiceTest.php b/Tests/Unit/Service/RecordServiceTest.php index 919de4f13..56492ef37 100644 --- a/Tests/Unit/Service/RecordServiceTest.php +++ b/Tests/Unit/Service/RecordServiceTest.php @@ -105,9 +105,12 @@ public function updateMethodCallsExpectedDatabaseMethod() $fields = ['foo' => 'bar', 'uid' => $uid]; $mock = $this->getMockServiceInstance(); - $this->createAndRegisterMockForQueryBuilder(); + $this->createAndRegisterMockForQueryBuilder([['uid' => 123]]); - self::assertFalse(empty($mock->update($table, $fields))); + self::assertSame( + 1, + $mock->update($table, $fields) + ); } /** @@ -119,7 +122,7 @@ public function deleteMethodCallsExpectedDatabaseMethodWithUid() $uid = 123; $mock = $this->getMockServiceInstance(); - $this->createAndRegisterMockForQueryBuilder(); + $this->createAndRegisterMockForQueryBuilder([['uid' => 123]]); $this->assertTrue( $mock->delete($table, $uid) @@ -135,7 +138,7 @@ public function deleteMethodCallsExpectedDatabaseMethodWithRecord() $record = ['uid' => 123]; $mock = $this->getMockServiceInstance(); - $this->createAndRegisterMockForQueryBuilder(); + $this->createAndRegisterMockForQueryBuilder([['uid' => 123]]); $this->assertTrue( $mock->delete($table, $record)