Valera Leontyev
Valera Leontyev

Reputation: 1181

Doctrine query cache & update

Doctrine 2 query caching was a little surprising for me. I had two update queries that follow one-by-one:

function upd($user, $passwordHash) {
    $qb = $this->entityManager->createQueryBuilder()
        ->update(UserEntity::__class, 'u')
        ->set('u.password', '?1')
        ->where('u = ?0')
        ->setParameters(array($user, $passwordHash));
    $qb->getQuery()->execute();
}

I updated password with 2 different values (ex. A, B), but user was the same:

upd($user, 'A');
upd($user, 'B');

The first query really updated database row. But the second just didn't make any changes in DB after execution.

Trying to solve this problem I found some workaround:

$qb->getQuery()->useQueryCache(false)->execute();

After disabling QueryCache both two queries changes DB row.

So, the questions are: why doctrine uses 1st query cache in 2nd UPDATE query? And why doctrine uses cache while it's two queries with different parameter ($passwordHash)?

Upvotes: 2

Views: 2085

Answers (1)

Valera Leontyev
Valera Leontyev

Reputation: 1181

Problem was found. It is an issue in doctrine 2 ORM code. My $user entity has inheritance, so the update uses Doctrine\ORM\Query\Exec\MultiTableUpdateExecutor. In that executor:

//FIXME (URGENT): With query cache the parameter is out of date. Move to execute() stage.

Source.

So the only workaround for now is to disable query cache for multitable updates:

$qb->getQuery()->useQueryCache(false)->execute();

I created new bug for it.

Upvotes: 2

Related Questions