Skip to content

Commit

Permalink
Locking
Browse files Browse the repository at this point in the history
  • Loading branch information
thorewi authored Dec 13, 2023
1 parent 4d37afc commit 99faad9
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/QueryObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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));
Expand Down Expand Up @@ -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();
Expand All @@ -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;
}
Expand Down

0 comments on commit 99faad9

Please sign in to comment.