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.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | taxonomy_node_save.patch | 504 bytes | nkurz |
Comments
Comment #1
nkurz commentedHere's the trivial untested patch in case that makes anything clearer.
Comment #2
killes@www.drop.org commentedWell, spotted. I don't think that the current code will corrupt the database, but it should get fixed nevertheless.
Comment #3
dries commentedCommitted to HEAD. Thanks!
Comment #4
(not verified) commented