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; }