1

D6 module https://www.drupal.org/project/uc_dependent_attributes is converted to D7 here: http://dupgrade.com/ and I found out db_affected_rows() was removed from D7.

I tried few codes, basically copy and paste, based on this answer db_affected_rows gives fatal error in drupal 7. Now I have a different error: Call to a member function rowCount() on a non-object.

Code which throws Call to undefined function db_affected_rows()

function uc_dependent_attributes_uc_product_adjustments_form_submit(&$form, &$form_state) {
  foreach ($form_state['values']['body'] as $value) {
    if (!$value['enabled']) {
     db_update('uc_dependent_attributes')->fields(array(
        'enabled' => 0,
      ))->condition('nid', $form_state['values']['nid'])->condition('combination', $value['combo_array'])->execute();
      if (!db_affected_rows()) {
       db_insert('uc_dependent_attributes')->fields(array(
          'nid' => $form_state['values']['nid'],
          'combination' => $value['combo_array'],
          'enabled' => 0,
        ))->execute();
      }
    }
    else {
      db_delete('uc_dependent_attributes')->condition('nid', $form_state['values']['nid'])->condition('combination', $value['combo_array'])->execute();
    }
  }
}

Changed: This gives 'Fatal error: Call to a member function rowCount() on a non-object' error.

function uc_dependent_attributes_uc_product_adjustments_form_submit(&$form, &$form_state) {
  foreach ($form_state['values']['body'] as $value) {
    if (!$value['enabled']) {
     $result = db_update('uc_dependent_attributes')->fields(array(
        'enabled' => 0,
      ))->condition('nid', $form_state['values']['nid'])->condition('combination', $value['combo_array'])->execute();
      $affected_rows = $result->rowCount();
      if ($affected_rows!=0) {
        db_insert('uc_dependent_attributes')->fields(array(
          'nid' => $form_state['values']['nid'],
          'combination' => $value['combo_array'],
          'enabled' => 0,
        ))->execute();
      }
    }
    else {
      db_delete('uc_dependent_attributes')->condition('nid', $form_state['values']['nid'])->condition('combination', $value['combo_array'])->execute();
    }
  }
}

How can I change db_affected_rows() to D7 properly?

1

1 Answer 1

0

As it happens UpdateQuery::execute() returns

The number of rows affected by the update.

So instead of:

$result = db_update('uc_dependent_attributes')->fields(array(
  'enabled' => 0,
))->condition('nid', $form_state['values']['nid'])->condition('combination', $value['combo_array'])->execute();
$affected_rows = $result->rowCount();
if ($affected_rows!=0) {

use:

...
$affected_rows = db_update('uc_dependent_attributes')
  ->fields(array('enabled' => 0))
  ->condition('nid', $form_state['values']['nid'])
  ->condition('combination', $value['combo_array'])
  ->execute();

if ($affected_rows == 0) {
...

(there's also a small fix to the conditional logic)

2
  • 1
    In general Clive you're right. But RETURN_AFFECTED src may not always return updated rows depending upon the schema configuration. Things like INSERT ... ON DUPLICATE KEY and also Updates to rows with identical values will not return a necessarily meaningful value for $affected_rows php.net/manual/en/pdostatement.rowcount.php I'm basically putting this as a comment as a public service announcement that rowCount() and its kin may be misleading.
    – tenken
    Commented Mar 11, 2015 at 2:37
  • i can save adjustment form in 'node/7/edit/adjustments' now without the above errors. thank you both for your time.
    – bellagio
    Commented Mar 12, 2015 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.