My recent log message file is getting loaded up with the following error:

Notice: Undefined variable: vid in _radioactivity_update_energy() (line 267 of /var/www/sites/all/modules/radioactivity/radioactivity.module).

It seems this happens when Radioactivity tries to write to the database. If Memcached storage is on, it shows these errors on Cron runs. If live storage is on it happens when emit.php is fired.

Currently running with: 7.x-2.9+5-dev

Here is the code in question:

/**
 * Update field energy for given entity_id+entity_type+bundle+field_name
 */
function _radioactivity_update_energy(
  $entity_type,
  $bundle,
  $field_name,
  $language,
  $entity_id,
  $energy_delta,
  $time,
  $force = FALSE
) {

  if ($entity_type == 'node') {
    $vid = db_query('SELECT vid FROM {node} n WHERE n.nid=:nid', array(':nid' => $entity_id))->fetchField();
  }

  $tables = array(
    "field_data_" . $field_name,
  );

  if ($vid) {       <------- Line 267
    $tables[] = "field_revision_" . $field_name;
  }
  else {
    $vid = $entity_id;
  }

  $revision_table_name = "field_revision_" . $field_name;

  $energy_column_name = $field_name . '_' . RADIOACTIVITY_FIELD_ENERGY;
  $timestamp_column_name = $field_name . '_' . RADIOACTIVITY_FIELD_TIMESTAMP;

  foreach ($tables as $table_name) {
    $q = db_merge($table_name)->key(array(
        'entity_type' => $entity_type,
        'bundle' => $bundle,
        'entity_id' => $entity_id,
        'language' => $language,
        'delta' => 0,
      ))->fields(array(
        'revision_id' => $vid,
        $timestamp_column_name => $time,
        $energy_column_name => $energy_delta,
      ));

    if (!$force) {
      // update instead of set the energy
      $q->expression($energy_column_name, $energy_column_name . ' + :inc', array(':inc' => $energy_delta));
    }

    $q->execute();
  }
}

I flagged the line in question for reference.
Everything works so its loading the $entity_id as the $vid from the else statement. I am not using revisions on my nodes.

A possible solution might be to pre-set the variable as NULL so it stops the error message from happening.

Comments

Ravenight’s picture

Simple fix to remove the errors is to declare the $vid.

This is what I did but I'm not sure if there are any negatives to doing it. Everything still works and I am no longer getting these errors.

function _radioactivity_update_energy(
  $entity_type,
  $bundle,
  $field_name,
  $language,
  $entity_id,
  $energy_delta,
  $time,
  $force = FALSE
) {

   $vid = FALSE;   <---- I ADDED THIS HERE

  if ($entity_type == 'node') {
    $vid = db_query('SELECT vid FROM {node} n WHERE n.nid=:nid', array(':nid' => $entity_id))->fetchField();
  }