private function _upgrade2 ()
    {
    	$craft = \Craft::$app;
    	$elements = $craft->elements;

    	// Delete the old plugin row
	    $this->delete(Table::PLUGINS, ['handle' => 'simple-map']);

	    // Update the old data
	    echo '    > Start map data upgrade' . PHP_EOL;

	    $rows = (new Query())
		    ->select('*')
		    ->from('{{%simplemap_maps}}')
		    ->all();

	    foreach ($rows as $row)
	    {
		    echo '    > Upgrade map value ' . $row['address'] . PHP_EOL;

	    	$site = $this->getSiteByLocale($row['ownerLocale']);

	    	$map = new MapElement([
			    'ownerId'     => $row['ownerId'],
			    'ownerSiteId' => $site->id,
			    'fieldId'     => $row['fieldId'],
			    'lat'         => $row['lat'],
			    'lng'         => $row['lng'],
			    'zoom'        => $row['zoom'] ?? 15,
			    'address'     => $row['address'],
			    'parts'       => Json::decodeIfJson($row['parts']),
		    ]);

		    $elements->saveElement($map, false);
	    }

	    $this->dropTable('{{%simplemap_maps}}');

	    // Update old field types
	    echo '    > Upgrade map field type upgrade' . PHP_EOL;

	    $rows = (new Query())
		    ->select(['id', 'settings', 'handle'])
		    ->from(Table::FIELDS)
		    ->where(['type' => 'SimpleMap_Map'])
		    ->all();

	    foreach ($rows as $row)
	    {
		    echo '    > Upgrade map field ' . $row['handle'] . PHP_EOL;

	    	$id = $row['id'];
	    	$oldSettings = Json::decodeIfJson($row['settings']);

	    	$newSettings = [
			    'lat'     => $oldSettings['lat'],
			    'lng'     => $oldSettings['lng'],
			    'zoom'    => $oldSettings['zoom'] ?? 15,
			    'country' => strtoupper($oldSettings['countryRestriction'] ?? '') ?: null,
			    'hideMap' => $oldSettings['hideMap'],
		    ];

		    $this->db->createCommand()
			    ->update(
			    	Table::FIELDS,
				    [
				    	'type' => MapField::class,
					    'settings' => Json::encode($newSettings),
				    ],
				    compact('id')
			    )
			    ->execute();
	    }

	    // Update the plugin settings
	    $this->updatePluginSettings();
    }
private function _upgrade2 ()
    {
    	$craft = \Craft::$app;
    	$elements = $craft->elements;

    	// Delete the old plugin row
	    $this->delete(Table::PLUGINS, ['handle' => 'simple-map']);

	    // Update the old data
	    echo '    > Start map data upgrade' . PHP_EOL;

	    $rows = (new Query())
		    ->select('*')
		    ->from('{{%simplemap_maps}}')
		    ->all();

	    foreach ($rows as $row)
	    {
		    echo '    > Upgrade map value ' . $row['address'] . PHP_EOL;

	    	$site = $this->getSiteByLocale($row['ownerLocale']);

	    	$map = new MapElement([
			    'ownerId'     => $row['ownerId'],
			    'ownerSiteId' => $site->id,
			    'fieldId'     => $row['fieldId'],
			    'lat'         => $row['lat'],
			    'lng'         => $row['lng'],
			    'zoom'        => $row['zoom'] ?? 15,
			    'address'     => $row['address'],
			    'parts'       => Json::decodeIfJson($row['parts']),
		    ]);

		    $elements->saveElement($map, false);
	    }

	    $this->dropTable('{{%simplemap_maps}}');

	    // Update old field types
	    echo '    > Upgrade map field type upgrade' . PHP_EOL;

	    $rows = (new Query())
		    ->select(['id', 'settings', 'handle'])
		    ->from(Table::FIELDS)
		    ->where(['type' => 'SimpleMap_Map'])
		    ->all();

	    foreach ($rows as $row)
	    {
		    echo '    > Upgrade map field ' . $row['handle'] . PHP_EOL;

	    	$id = $row['id'];
	    	$oldSettings = Json::decodeIfJson($row['settings']);

	    	$newSettings = [
			    'lat'     => $oldSettings['lat'],
			    'lng'     => $oldSettings['lng'],
			    'zoom'    => $oldSettings['zoom'] ?? 15,
			    'country' => strtoupper($oldSettings['countryRestriction'] ?? '') ?: null,
			    'hideMap' => $oldSettings['hideMap'],
		    ];

		    $this->db->createCommand()
			    ->update(
			    	Table::FIELDS,
				    [
				    	'type' => MapField::class,
					    'settings' => Json::encode($newSettings),
				    ],
				    compact('id')
			    )
			    ->execute();
	    }

	    // Update the plugin settings
	    $this->updatePluginSettings();
    }
private function _upgrade2 ( ) { $ craft = \ Craft :: $ app ; $ elements = $ craft -> elements ; // Delete the old plugin row $ this -> delete ( Table :: PLUGINS , [ 'handle' => 'simple-map' ] ) ; // Update the old data echo '    > Start map data upgrade' . PHP_EOL ; $ rows = ( new Query ( ) ) -> select ( '*' ) -> from ( '{{%simplemap_maps}}' ) -> all ( ) ; foreach ( $ rows as $ row ) { echo '    > Upgrade map value ' . $ row [ 'address' ] . PHP_EOL ; $ site = $ this -> getSiteByLocale ( $ row [ 'ownerLocale' ] ) ; $ map = new MapElement ( [ 'ownerId' => $ row [ 'ownerId' ] , 'ownerSiteId' => $ site -> id , 'fieldId' => $ row [ 'fieldId' ] , 'lat' => $ row [ 'lat' ] , 'lng' => $ row [ 'lng' ] , 'zoom' => $ row [ 'zoom' ] ?? 15 , 'address' => $ row [ 'address' ] , 'parts' => Json :: decodeIfJson ( $ row [ 'parts' ] ) , ] ) ; $ elements -> saveElement ( $ map , false ) ; } $ this -> dropTable ( '{{%simplemap_maps}}' ) ; // Update old field types echo '    > Upgrade map field type upgrade' . PHP_EOL ; $ rows = ( new Query ( ) ) -> select ( [ 'id' , 'settings' , 'handle' ] ) -> from ( Table :: FIELDS ) -> where ( [ 'type' => 'SimpleMap_Map' ] ) -> all ( ) ; foreach ( $ rows as $ row ) { echo '    > Upgrade map field ' . $ row [ 'handle' ] . PHP_EOL ; $ id = $ row [ 'id' ] ; $ oldSettings = Json :: decodeIfJson ( $ row [ 'settings' ] ) ; $ newSettings = [ 'lat' => $ oldSettings [ 'lat' ] , 'lng' => $ oldSettings [ 'lng' ] , 'zoom' => $ oldSettings [ 'zoom' ] ?? 15 , 'country' => strtoupper ( $ oldSettings [ 'countryRestriction' ] ?? '' ) ? : null , 'hideMap' => $ oldSettings [ 'hideMap' ] , ] ; $ this -> db -> createCommand ( ) -> update ( Table :: FIELDS , [ 'type' => MapField :: class , 'settings' => Json :: encode ( $ newSettings ) , ] , compact ( 'id' ) ) -> execute ( ) ; } // Update the plugin settings $ this -> updatePluginSettings ( ) ; }
Upgrade from Craft 2

@throws \Throwable
@throws \craft\errors\ElementNotFoundException
@throws \yii\base\Exception
@throws \yii\db\Exception
Upgrade from Craft 2