As I was going through #226876: DBTNG poll.module I noticed that a query I tried to rewrite with DBTNG failed:

Original

db_query("UPDATE {poll_choices} SET chvotes = chvotes + 1 WHERE chid = %d", $choice);

Rewrite:

db_update('poll_choices')
  ->expression('chvotes', 'chvotes + 1')
  ->condition('chid', $choice)
  ->execute();

The latter should be perfectly valid, but I get a PDO exception:
Invalid argument supplied for foreach() Warning query.inc 916 UpdateQuery->execute()
Invalid argument supplied for foreach() Warning query.inc 939 UpdateQuery->__toString()

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Dave Reid’s picture

Turns out this is an easy fix. The class variable $fields in UpdateQuery is NULL, and so when the query is run, it tries to run a foreach using $fields. Just setting the $fields default to array() fixes this completely. I have provided a test for the expression-only update query.

Dave Reid’s picture

Status: Active » Needs review

Marking code needs review.

Dave Reid’s picture

Reposting without the try-catch in the test. Discussed with chx on #drupal and found it unnecessary.

Dave Reid’s picture

Status: Needs review » Postponed

This is required for #226876: DBTNG poll.module to land in HEAD. Please review.

Dave Reid’s picture

Issue-status changing fail. My bad.

chx’s picture

Status: Postponed » Reviewed & tested by the community

A simple change and the bot is happy.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

Nice! I love one-liner fixes that come with tests. :)

Committed to HEAD, thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for two weeks with no activity.