Revell
Revell

Reputation: 723

Drupal db_update not working, unless intentional error through batch API

I'm submitting a form from a view through the Drupal batch API to update a row in my DB. The statement I use for that is:

db_update('scores')
    ->fields(['status' => 0])
    ->condition('sid', $score->sid)
    ->execute();

The batch returns successfully and gives me the feedback of

Performed undo publishing on 1 item.

However, the row in the DB is not updated.

When using the following code:

$result = db_update('scores')
    ->fields(['status' => 0])
    ->condition('sid', $score->sid)
    ->execute();

drq($result);

The batch API returns an error due to unexpected output, and after refreshing the page manually, the row in question is updated!

I can't for the life of me figure out what's going on nor how to get the query from the batch API to log somewhere so I can see what's going on.

Any help is much appreciated.

Upvotes: 0

Views: 103

Answers (1)

Revell
Revell

Reputation: 723

It seems that there's two queries being executed. One by the db_update statement described earlier, and one by entity.controller.inc::save() in the entity module. The latter started a transaction which negated the db_update action.

I now changed the code to the following

db_update('scores')
    ->fields(['status' => 0])
    ->condition('sid', $score->sid)
    ->execute();

$score->status = 0;
$score->save();

And it works as intended. The status of the item gets changed in DB and all seems well. I know this is an ugly workaround for the actual problem, but at this moment it's the only solution I can find.

Upvotes: 0

Related Questions