diff --git a/htdocs/categories/categorie.class.php b/htdocs/categories/categorie.class.php index 13247d3f659..766c7ae06b8 100644 --- a/htdocs/categories/categorie.class.php +++ b/htdocs/categories/categorie.class.php @@ -566,11 +566,12 @@ class Categorie return -1; } - // On ajoute la propriete fullpath a tous les éléments + // We add the fulpath property to each elements of first level (no parent exists) + dolibarr_syslog("Categorie::get_full_arbo call to build_path_from_id_categ", LOG_DEBUG); foreach($this->cats as $key => $val) { if (isset($motherof[$key])) continue; - $this->build_path_from_id_categ($key,0); // Process a path of a root category (no mother exists) + $this->build_path_from_id_categ($key,0); // Process a path of a root category (no parent exists) } dolibarr_syslog("Categorie::get_full_arbo dol_sort_array", LOG_DEBUG); @@ -588,6 +589,8 @@ class Categorie */ function build_path_from_id_categ($id_categ,$protection=0) { + dolibarr_syslog("Categorie::build_path_from_id_categ id_categ=".$id_categ." protection=".$protection, LOG_DEBUG); + // Defini fullpath if (isset($this->cats[$id_categ]['id_mere'])) { @@ -610,6 +613,13 @@ class Categorie if (! is_array($this->cats[$id_categ]['id_children'])) return; foreach($this->cats[$id_categ]['id_children'] as $key => $idchild) { + // Protection when a category has itself as a child (should not happen) + if ($idchild == $id_categ) + { + dolibarr_syslog("Categorie::build_path_from_id_categ bad couple (".$idchild.",".$id_categ.") in association table: An entry should not have itself has child", LOG_WARNING); + continue; + } + $this->build_path_from_id_categ($idchild,$protection); } return; diff --git a/mysql/migration/2.5.0-2.6.0.sql b/mysql/migration/2.5.0-2.6.0.sql index 4a65edf5a4c..2864bf41313 100644 --- a/mysql/migration/2.5.0-2.6.0.sql +++ b/mysql/migration/2.5.0-2.6.0.sql @@ -59,3 +59,8 @@ alter table llx_user_param drop column page; alter table llx_commande_fournisseur_log add column comment varchar(255) NULL; +delete from llx_categorie_association where fk_categorie_mere = fk_categorie_fille; + + +-- Put at the end. Cas have duplicate values +ALTER TABLE llx_categorie_association ADD UNIQUE INDEX uk_categorie_association (fk_categorie_mere, fk_categorie_fille); diff --git a/mysql/tables/llx_categorie_association.key.sql b/mysql/tables/llx_categorie_association.key.sql index 383899dea34..0a4d78a440c 100644 --- a/mysql/tables/llx_categorie_association.key.sql +++ b/mysql/tables/llx_categorie_association.key.sql @@ -24,5 +24,7 @@ ALTER TABLE llx_categorie_association ADD INDEX idx_categorie_association_fk_categorie_mere (fk_categorie_mere); ALTER TABLE llx_categorie_association ADD INDEX idx_categorie_association_fk_categorie_fille (fk_categorie_fille); +ALTER TABLE llx_categorie_association ADD UNIQUE INDEX uk_categorie_association (fk_categorie_mere, fk_categorie_fille); + ALTER TABLE llx_categorie_association ADD CONSTRAINT fk_categorie_asso_fk_categorie_mere FOREIGN KEY (fk_categorie_mere) REFERENCES llx_categorie (rowid); ALTER TABLE llx_categorie_association ADD CONSTRAINT fk_categorie_asso_fk_categorie_fille FOREIGN KEY (fk_categorie_fille) REFERENCES llx_categorie (rowid);