Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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()
Comment | File | Size | Author |
---|---|---|---|
#3 | 343620-db-update-expression-D7.patch | 1.75 KB | Dave Reid |
#1 | 343620-db-update-expression-D7.patch | 1.86 KB | Dave Reid |
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 CreditAttribution: 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!