2 use Migrations\AbstractMigration;
4 use Cake\ORM\TableRegistry;
7 * this is an unusual migration since we want it to turn old data in latlong column into
8 * new data in two columns, lat and long.
10 class TransmogrifyLatLongDataInLocations extends AbstractMigration {
14 * More information on this method is available here:
15 * http://docs.phinx.org/en/latest/migrations.html#the-change-method
19 // public function change()
24 * handle migrating the single column into two.
25 * new columns must already exist.
26 * old latlong column could be blank if we're doing a new db, so we have to handle that.
28 public function up() {
29 $locations = TableRegistry::get ( 'Locations' );
31 $query = $locations->find ();
33 // iterate across table on latlong.
34 foreach ( $query as $locat ) {
35 Log::write ( 'debug', 'id=' . var_export ( $locat->id, true ) . ' L&L=' . var_export ( $locat->latlong, true ) );
36 // do nothing if the field is empty.
37 if ($locat->latlong) {
38 // break out the two pieces.
39 $latlongExploded = explode ( ',', $locat->latlong );
40 Log::write ( 'debug', 'broke into lat=' . $latlongExploded [0] . ' lng=' . $latlongExploded [1] );
42 // now update the row where we got this info with same lat and long values in two columns.
43 $loc_update = $locations->get ( $locat->id );
44 $loc_update->lat = floatval ( $latlongExploded [0] );
45 $loc_update->lng = floatval ( $latlongExploded [1] );
46 $locations->save ( $loc_update );
52 * performs the reverse case of migrating the two columns back into one.
53 * 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.
55 public function down() {
56 $locations = TableRegistry::get ( 'Locations' );
58 $query = $locations->find ();
60 // iterate across table on lat and long values.
61 foreach ( $query as $locat ) {
62 Log::write ( 'debug', 'id=' . var_export ( $locat->id, true ) . ' lat=' . var_export ( $locat->lat, true )
63 .' lng=' . var_export ( $locat->lng, true ));
64 // do nothing if the fields are empty.
65 if ($locat->lat && $locat->lng) {
66 // combine the two pieces.
67 $latlong = '' . $locat->lat . ',' . $locat->lng;
68 Log::write ( 'debug', 'combo is=' . $latlong);
70 // now update the row where we got these values to reproduce the single column.
71 $loc_update = $locations->get ( $locat->id );
72 $loc_update->latlong = $latlong;
73 $locations->save ( $loc_update );