@@ -112,13 +112,10 @@ public function updateCollection($collection)
112112
113113 }
114114
115- public function setFields ($ collectionName , $ fields )
115+ public function renameColumns ($ collectionName , $ fields )
116116 {
117117 $ collection = $ this ->selectCollection ($ collectionName );
118- /*
119- * Rename Columns
120- */
121- $ renames = [];
118+ $ renames = [];
122119 foreach ($ fields as $ field ) {
123120 if ($ field ->oldName != "" ) {
124121 if ($ field ->oldName != $ field ->name ) {
@@ -131,19 +128,23 @@ public function setFields($collectionName, $fields)
131128 if (count ($ renames ) > 0 ) {
132129 $ update ['$rename ' ] = $ renames ;
133130 $ collection ->updateMany (array (), $ update , array ('upsert ' => true ));
131+ $ dbmCollection = DBM_Collection::where ('name ' , $ collectionName )->first ();
134132 foreach ($ renames as $ oldName => $ newName ) {
135- $ collection_field = CollectionField::where ('old_name ' , $ oldName )->first ();
133+ $ collection_field = CollectionField::where ([
134+ 'dbm_collection_id ' => $ dbmCollection ->_id ,
135+ 'old_name ' => $ oldName ,
136+ ])->first ();
136137 $ collection_field ->old_name = $ newName ;
137138 $ collection_field ->update ();
138139 }
139140 }
141+ }
140142
141- $ newFields = $ this ->getColumnsName ($ collectionName );
142- $ columns = $ this ->getCollectionColumns ($ collectionName );
143-
144- /*
145- * Add Columns
146- */
143+ public function addColumns ($ collectionName )
144+ {
145+ $ collection = $ this ->selectCollection ($ collectionName );
146+ $ newFields = $ this ->getColumnsName ($ collectionName );
147+ $ update = [];
147148
148149 if ($ collection ->count () > 0 ) {
149150 foreach ($ newFields as $ newField ) {
@@ -176,14 +177,16 @@ public function setFields($collectionName, $fields)
176177 }
177178
178179 }
180+ }
179181
180- /*
181- * Remove Columns
182- */
183-
184- $ update = [];
182+ public function removeColumns ($ collectionName )
183+ {
184+ $ collection = $ this ->selectCollection ($ collectionName );
185+ $ newFields = $ this ->getColumnsName ($ collectionName );
186+ $ columns = $ this ->getCollectionColumns ($ collectionName );
187+ $ update = [];
188+ $ unsets = [];
185189
186- $ unsets = [];
187190 foreach ($ columns as $ column ) {
188191 if (!in_array ($ column , $ newFields )) {
189192 $ unsets [$ column ] = "" ;
@@ -194,7 +197,23 @@ public function setFields($collectionName, $fields)
194197 $ update ['$unset ' ] = $ unsets ;
195198 $ collection ->updateMany (array (), $ update , array ('upsert ' => true ));
196199 }
200+ }
201+
202+ public function setFields ($ collectionName , $ fields )
203+ {
204+ /*
205+ * Rename Columns
206+ */
207+ $ this ->renameColumns ($ collectionName , $ fields );
197208
209+ /*
210+ * Add Columns
211+ */
212+ $ this ->addColumns ($ collectionName );
213+ /*
214+ * Remove Columns
215+ */
216+ $ this ->removeColumns ($ collectionName );
198217 return true ;
199218 }
200219
0 commit comments