I'm brand new to Drupal and really rusty with PHP, but I just did a CVS checkout and was looking through taxonomy.module wondering how I could go about adding weights to taxonomy terms (http://drupal.org/node/18824) and think I've found a small logic error in taxonomy_node_save(). Or else I'm misunderstanding some really simple PHP. In any case, it seemed worth reporting so that someone familiar with the module could at least take a quick look.

function taxonomy_node_save($nid, $terms) {
  taxonomy_node_delete($nid);

  if (is_array($terms)) {
    foreach ($terms as $term) {
      if (is_array($term)) {
        foreach ($term as $tid) {
          if ($tid) {
            db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $tid);
          }
        }
      }
      if ($term) {
        db_query('INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)', $nid, $term);
      }
    }
  }
}

Walkthrough: taxonomy_node_save is passed an integer $nid and an array $terms. Each array element in $terms is either an array of tid's or a single integer tid. If the element is an array of tid's, we insert each (nid, tid) pair into {term_node}. But then after doing this, it looks like we also try to insert (nid,ARRAY) into {term_node} after we finish the foreach loop.

Is this perhaps supposed to be 'else if ($term)'? Or is 'if(ARRAY)' false in PHP? Apologies for making a bad first impression if I'm just dead wrong here.

CommentFileSizeAuthor
#1 taxonomy_node_save.patch504 bytesnkurz

Comments

nkurz’s picture

StatusFileSize
new504 bytes

Here's the trivial untested patch in case that makes anything clearer.

killes@www.drop.org’s picture

Well, spotted. I don't think that the current code will corrupt the database, but it should get fixed nevertheless.

dries’s picture

Committed to HEAD. Thanks!

Anonymous’s picture