Use case:
Drupal 6.12
Content Taxonomy RC1
Stock page node type with one required CT field using checkboxes, max 3 values allowed, values are also stored in term_node table.
Create first revision with 3 values for CT field, second revision with just one value. Attempt to revert to the first revision and you'll end up with something like:

warning: Invalid argument supplied for foreach() in content_taxonomy\content_taxonomy.module on line 493.
user warning: Duplicate entry '38-24457' for key 'PRIMARY' query: INSERT INTO term_node (nid, vid, tid) VALUES (5245, 24457, 38) in taxonomy\taxonomy.module on line 693.
user warning: Duplicate entry '1204-24457' for key 'PRIMARY' query: INSERT INTO term_node (nid, vid, tid) VALUES (5245, 24457, 1204) in taxonomy\taxonomy.module on line 693.
user warning: Duplicate entry '37-24457' for key 'PRIMARY' query: INSERT INTO term_node (nid, vid, tid) VALUES (5245, 24457, 37) in taxonomy.module on line 693.

One thing I've noticed is that the first param for _content_taxonomy_taxonomy_unset() called by content_taxonomy_field() in this case is an array structured like array(3) { [0]=> int(38) [1]=> int(1204) [2]=> int(37) } which means that in the foreach loop none of the conditions apply.

Investigating further....

Comments

Cologner875’s picture

i get the same warnings using 2 widget types of CT fields for one content type: autocomplete (freetagging) and checkboxes. nevertheless the content of the fields is reverted correctly.

the messages also show up if the content of the CT fields is not changed between two revisions.

nonsie’s picture

Title: Reverting a node causes duplicate term inserts, not all terms are unset in _content_taxonomy_taxonomy_unset » Reverting a node causes duplicate term inserts when node term is also stored in term_node
Status: Active » Needs review
StatusFileSize
new8.64 KB

For my example there's two fields, voc with id 2 and voc with id 3.

What seems to be happening here is the following:
1. For each content taxonomy field that also stores its data in term_node table the $node->taxonomy array consists of term IDs for the specific field like so:

array(
0 => 38,
1 => 37,
);

At the moment it is assumed that they are in an array where array key is vocabulary id like so:

array(
2 => array(0 => 38),
3 => array(0 => 37),
);

Core taxonomy also expects them to be in such format.

Attached patch converts $node->taxonomy from using term IDs as keys (code sample #1) to vocabulary keys (code sample #2).
I've done a fair share of testing and it seems to work - any help and suggestions are welcome as I'm not that familiar with content taxonomy.

nonsie’s picture

StatusFileSize
new8.94 KB

Updated patch attached

OV2’s picture

StatusFileSize
new904 bytes

With your patch enabled some of the taxonomy terms were no longer saved to the node_term table ( unset($node->taxonomy[$key]); can remove a vocabulary if you have multiple content taxonomy fields in one content type).

I've made a smaller patch that should also fix the issues:

"Invalid argument supplied for foreach()" is fixed by replacing "==" with "==="

The double inserts are fixed by ignoring terms that are not keyed by their vocabulary (similar to the is_object check already present).

mh86’s picture

Status: Needs review » Fixed

Hi!

I committed the fix from #4 - slightly different code, but does the same (see diff)
Thanks for bug report and patches!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.