X-Git-Url: https://feistymeow.org/gitweb/?a=blobdiff_plain;f=production%2Fexample_apps%2Fzippy_maps%2Fconfig%2FMigrations%2F20170712142151_TransmogrifyLatLongDataInLocations.php;fp=production%2Fexample_apps%2Fzippy_maps%2Fconfig%2FMigrations%2F20170712142151_TransmogrifyLatLongDataInLocations.php;h=d62aad2a589ef3fc0b8479aee10147d5c49dfa03;hb=34d1cb2e8687b826357db1d3821bf9e05cf6f13d;hp=0000000000000000000000000000000000000000;hpb=21f30bb859e6c15393e23ac0e5ef417b82f628a5;p=feisty_meow.git diff --git a/production/example_apps/zippy_maps/config/Migrations/20170712142151_TransmogrifyLatLongDataInLocations.php b/production/example_apps/zippy_maps/config/Migrations/20170712142151_TransmogrifyLatLongDataInLocations.php new file mode 100644 index 00000000..d62aad2a --- /dev/null +++ b/production/example_apps/zippy_maps/config/Migrations/20170712142151_TransmogrifyLatLongDataInLocations.php @@ -0,0 +1,77 @@ +find (); + + // iterate across table on latlong. + foreach ( $query as $locat ) { + Log::write ( 'debug', 'id=' . var_export ( $locat->id, true ) . ' L&L=' . var_export ( $locat->latlong, true ) ); + // do nothing if the field is empty. + if ($locat->latlong) { + // break out the two pieces. + $latlongExploded = explode ( ',', $locat->latlong ); + Log::write ( 'debug', 'broke into lat=' . $latlongExploded [0] . ' lng=' . $latlongExploded [1] ); + + // now update the row where we got this info with same lat and long values in two columns. + $loc_update = $locations->get ( $locat->id ); + $loc_update->lat = floatval ( $latlongExploded [0] ); + $loc_update->lng = floatval ( $latlongExploded [1] ); + $locations->save ( $loc_update ); + } + } + } + + /** + * performs the reverse case of migrating the two columns back into one. + * if we don't do this, we can't slide up and down the migration and rollback scale properly while keeping our lat and long data. + */ + public function down() { + $locations = TableRegistry::get ( 'Locations' ); + + $query = $locations->find (); + + // iterate across table on lat and long values. + foreach ( $query as $locat ) { + Log::write ( 'debug', 'id=' . var_export ( $locat->id, true ) . ' lat=' . var_export ( $locat->lat, true ) + .' lng=' . var_export ( $locat->lng, true )); + // do nothing if the fields are empty. + if ($locat->lat && $locat->lng) { + // combine the two pieces. + $latlong = '' . $locat->lat . ',' . $locat->lng; + Log::write ( 'debug', 'combo is=' . $latlong); + + // now update the row where we got these values to reproduce the single column. + $loc_update = $locations->get ( $locat->id ); + $loc_update->latlong = $latlong; + $locations->save ( $loc_update ); + } + } + } +}