Ville Mattila
Ville Mattila

Reputation: 1343

How to fetch entities from database and lock corresponding database rows in Doctrine ORM?

Is it possible (and how) in Doctrine 2 to create a query with a QueryBuilder and acquire a write lock for each of the matching rows? LockMode::PESSIMISTIC_WRITE can be used when fetching individual items with EntityRepository->find(), but I haven't been able to find such property for QueryBuilder.

Upvotes: 1

Views: 4392

Answers (1)

Ville Mattila
Ville Mattila

Reputation: 1343

The answer is to call setLockMode() on the Query object.

$qb = $em->createQueryBuilder();
$query = $qb->getQuery();

$query->setLockMode(LockMode::PESSIMISTIC_WRITE);

$results = $query->getResult();

The very last line of the Transactions and Concurrency documentation page shows that Query->setLockMode() is supported. A bit hard to notice, but it's there... ;)

Upvotes: 2

Related Questions