From 9ce5e6f3f197fe2a6e3c785db860c1e78420c55f Mon Sep 17 00:00:00 2001 From: Maxim Smakouz Date: Fri, 29 Mar 2024 11:33:43 +0200 Subject: [PATCH] Add method forUpdate (#465) --- src/Select/Repository.php | 8 ++++++++ .../Driver/SQLite/Select/RepositoryTest.php | 16 ++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/src/Select/Repository.php b/src/Select/Repository.php index 56e4ee74d..06aa58100 100644 --- a/src/Select/Repository.php +++ b/src/Select/Repository.php @@ -59,4 +59,12 @@ public function select(): Select { return clone $this->select; } + + public function forUpdate(): static + { + $repository = clone $this; + $repository->select->forUpdate(); + + return $repository; + } } diff --git a/tests/ORM/Functional/Driver/SQLite/Select/RepositoryTest.php b/tests/ORM/Functional/Driver/SQLite/Select/RepositoryTest.php index 48dae207f..ba16f2290 100644 --- a/tests/ORM/Functional/Driver/SQLite/Select/RepositoryTest.php +++ b/tests/ORM/Functional/Driver/SQLite/Select/RepositoryTest.php @@ -5,6 +5,7 @@ namespace Cycle\ORM\Tests\Functional\Driver\SQLite\Select; // phpcs:ignore +use Cycle\ORM\Tests\Fixtures\User; use Cycle\ORM\Tests\Functional\Driver\Common\Select\RepositoryTest as CommonClass; /** @@ -14,4 +15,19 @@ class RepositoryTest extends CommonClass { public const DRIVER = 'sqlite'; + + /** + * This test does not need to be executed for each database driver. + */ + public function testForUpdate(): void + { + $repository = $this->orm->getRepository(User::class); + + $this->assertFalse($repository->select()->getBuilder()->getQuery()->getTokens()['forUpdate']); + + $forUpdate = $repository->forUpdate(); + + $this->assertTrue($forUpdate->select()->getBuilder()->getQuery()->getTokens()['forUpdate']); + $this->assertNotSame($repository, $forUpdate); + } }