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()
Comments
Comment #1
dave reidTurns 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.
Comment #2
dave reidMarking code needs review.
Comment #3
dave reidReposting without the try-catch in the test. Discussed with chx on #drupal and found it unnecessary.
Comment #4
dave reidThis is required for #226876: DBTNG poll.module to land in HEAD. Please review.
Comment #5
dave reidIssue-status changing fail. My bad.
Comment #6
chx commentedA simple change and the bot is happy.
Comment #7
webchickNice! I love one-liner fixes that come with tests. :)
Committed to HEAD, thanks!