Duplicate entry in category_hierarchy (when saving category under container)

morningtime - May 18, 2009 - 12:27
Project:Category
Version:6.x-2.0-beta2
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

When saving a category under a container, an error occurs:

"user warning: Duplicate entry '57-0' for key 'PRIMARY' query: INSERT INTO category_hierarchy (cid, parent) VALUES (57, 0) in C:\Inetpub\wwwroot\sites\all\modules\category\category.module on line 1172."

In table category_hierarchy I now see:

cid - parent
57 - 0
57 - 55

(55 is here the parent container ID)

The problem seems to be that during re-saving of the category it forgets that it already has a category_hierarchy entry. It thinks is has no parent now, and saves 57-0. Due to this, in the "list categories" page, you now see two entries of the same category.

  if (is_array($node->category['parents'])) {
    foreach ($node->category['parents'] as $parent) {
      if (is_array($parent)) {
        foreach ($parent as $cid) {
          db_query('INSERT INTO {category_hierarchy} (cid, parent) VALUES (%d, %d)', $node->category['cid'], $cid);
        }
      }
      else if (is_object($parent)) {
        db_query('INSERT INTO {category_hierarchy} (cid, parent) VALUES (%d, %d)', $node->category['cid'], $parent->cid);
      }
      else {
1172 --->      db_query('INSERT INTO {category_hierarchy} (cid, parent) VALUES (%d, %d)', $node->category['cid'], $parent);
      }
    }
  }

It seems to be a complex problem! The db_query on line 1172 is called twice as it loops through the parents. But somehow it thinks there should also be a parent = 0. Which is false, because the category only belongs to parent=55 (container) and not to root (0).

A workaround suggestion, perhaps:

      else {
       if ($parent != 0) { // added
1172 --->      db_query('INSERT INTO {category_hierarchy} (cid, parent) VALUES (%d, %d)', $node->category['cid'], $parent);
       }
      } // added

Does that work in all cases?

#1

morningtime - May 18, 2009 - 12:28
Status:active» needs review

#2

morningtime - May 18, 2009 - 12:32

So the real problem is that "$node->category['parents']" includes a "0" that should not exist there, because the re-saved category also has a parent=55.

#3

JirkaRybka - May 18, 2009 - 19:33
Status:needs review» active

I can't reproduce this, however hard I try. (Found two unrelated bugs along the way, but no duplicates on my config...) So more information would be nice here, i.e. what and where are you doing, which configuration (sub-modules, settings etc.), so that we can reproduce the problem (and track it down then).

BTW - note to everyone: Don't do previews on categories and containers! That's a really bad idea, as the previews are broken like hell (container node gets a category fieldset, category node changes parent container/category to the first in list etc.) Considering how much problems there are with previews, and how little are they useful on categories and containers (=not ordinary content), I would say - just hook_form_alter() the preview button away. Sorry for the sidenote, I don't want to hijack the issue.

 
 

Drupal is a registered trademark of Dries Buytaert.