@@ -41,7 +41,7 @@ public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $
41
41
/**
42
42
* Clean up duplicate categories
43
43
*/
44
- private function cleanupDuplicateCategories (IOutput $ output ) {
44
+ private function cleanupDuplicateCategories (IOutput $ output ): void {
45
45
$ output ->info ('Starting cleanup of duplicate vcategory records... ' );
46
46
47
47
// Find all categories, ordered to identify duplicates
@@ -74,6 +74,8 @@ private function cleanupDuplicateCategories(IOutput $output) {
74
74
75
75
$ output ->info ("Found duplicate: keeping ID $ keepId, removing ID $ categoryId " );
76
76
77
+ $ this ->cleanupDuplicateAssignments ($ output , $ categoryId , $ keepId );
78
+
77
79
// Update object references
78
80
$ updateQb = $ this ->connection ->getQueryBuilder ();
79
81
$ updateQb ->update ('vcategory_to_object ' )
@@ -103,4 +105,41 @@ private function cleanupDuplicateCategories(IOutput $output) {
103
105
$ output ->info ("Duplicate cleanup completed - processed $ duplicateCount duplicates " );
104
106
}
105
107
}
108
+
109
+ /**
110
+ * Clean up duplicate assignments
111
+ * That will delete rows with $categoryId when there is the same row with $keepId
112
+ */
113
+ private function cleanupDuplicateAssignments (IOutput $ output , int $ categoryId , int $ keepId ): void {
114
+ $ selectQb = $ this ->connection ->getQueryBuilder ();
115
+ $ selectQb ->select ('o1.* ' )
116
+ ->from ('vcategory_to_object ' , 'o1 ' )
117
+ ->join (
118
+ 'o1 ' , 'vcategory_to_object ' , 'o2 ' ,
119
+ $ selectQb ->expr ()->andX (
120
+ $ selectQb ->expr ()->eq ('o1.type ' , 'o2.type ' ),
121
+ $ selectQb ->expr ()->eq ('o1.objid ' , 'o2.objid ' ),
122
+ )
123
+ )
124
+ ->where ($ selectQb ->expr ()->eq ('o1.categoryid ' , $ selectQb ->createNamedParameter ($ categoryId )))
125
+ ->andWhere ($ selectQb ->expr ()->eq ('o2.categoryid ' , $ selectQb ->createNamedParameter ($ keepId )));
126
+
127
+ $ deleteQb = $ this ->connection ->getQueryBuilder ();
128
+ $ deleteQb ->delete ('vcategory_to_object ' )
129
+ ->where ($ deleteQb ->expr ()->eq ('objid ' , $ deleteQb ->createParameter ('objid ' )))
130
+ ->andWhere ($ deleteQb ->expr ()->eq ('categoryid ' , $ deleteQb ->createParameter ('categoryid ' )))
131
+ ->andWhere ($ deleteQb ->expr ()->eq ('type ' , $ deleteQb ->createParameter ('type ' )));
132
+
133
+ $ duplicatedAssignments = $ selectQb ->executeQuery ();
134
+ $ count = 0 ;
135
+ while ($ row = $ duplicatedAssignments ->fetch ()) {
136
+ $ deleteQb
137
+ ->setParameters ($ row )
138
+ ->executeStatement ();
139
+ $ count ++;
140
+ }
141
+ if ($ count > 0 ) {
142
+ $ output ->info (" - Deleted $ count duplicate category assignments for $ categoryId and $ keepId " );
143
+ }
144
+ }
106
145
}
0 commit comments