From 99faad926cbdd92c448d0ee64fc7f88f8be367e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Kud=C4=9Blka?= Date: Wed, 13 Dec 2023 08:38:08 +0100 Subject: [PATCH] Locking --- src/QueryObject.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/QueryObject.php b/src/QueryObject.php index f4f0daf..64cd701 100644 --- a/src/QueryObject.php +++ b/src/QueryObject.php @@ -480,7 +480,7 @@ private function isAlreadyJoined(string $filterKey): bool * @throws ReflectionException * @throws Exception */ - final public function fetch(?int $limit = null, ?int $offset = null): array + final public function fetch(?int $limit = null, ?int $offset = null, bool $lock = false): array { $qb = $this->createQueryBuilder(); @@ -498,6 +498,10 @@ final public function fetch(?int $limit = null, ?int $offset = null): array $query->setFirstResult($offset); } + if ($lock) { + $query->setLockMode(LockMode::PESSIMISTIC_WRITE); + } + $result = $query->getResult(); $this->postFetch(new ArrayIterator($result)); @@ -526,9 +530,9 @@ final public function fetchIterable(): Generator * @throws NonUniqueResultException * @throws ReflectionException */ - final public function fetchOne(bool $strict = true): object + final public function fetchOne(bool $strict = true, bool $lock = false): object { - $result = $this->fetch(2); + $result = $this->fetch(2, $lock); if (!$result) { throw new NoResultException(); @@ -548,10 +552,10 @@ final public function fetchOne(bool $strict = true): object * @throws NonUniqueResultException * @throws ReflectionException */ - final public function fetchOneOrNull(bool $strict = true): object|null + final public function fetchOneOrNull(bool $strict = true, bool $lock = false): object|null { try { - return $this->fetchOne($strict); + return $this->fetchOne($strict, $lock); } catch (NoResultException) { return null; }