Migrate addressfield: Call to a member function import() on a non-object in migrate/includes/migration.inc on line 696 also posted to Migrate issue queue as a support request

I am importing users and data from csv with a Migrate Group. The initial user creation and some additional data in other tables in working fine.

I have read everything I can find on Google about Migrate and Addressfield, and some of it is 2 years old and confusing. For example, Migrate has deprecated $arguments for field mappings, sub-fields are mapped with colon notation. Also, I am adding the Addressfield migration to a group so it gets the correct uid from the completed user migration, so posts about direct csv imports are not helpful.

I am getting a message when I migrate (with drush)

PHP Fatal error: Call to a member function import() on a non-object in /var/www/[site]/sites/all/modules/contrib/migrate/includes/migration.inc on line 696

I put a drush_print_r on that line

drush_print_r($this->destinationValues);drush_print_r($this->sourceValues);

the output of drush_print_r just before the error is



stdClass Object
(
    [entity_id] =&gt; 483
    [field_user_address] =&gt; Array
        (
            [0] =&gt; US
            [arguments] =&gt; Array
                (
                    [administrative_area] =&gt; NY
                    [first_name] =&gt; [actual first name]
                    [last_name] =&gt; [actual last name]
                    [locality] =&gt; New York
                    [postal_code] =&gt; 10028
                    [premise] =&gt; 
                    [thoroughfare] =&gt; [actual address]
                )

        )

    [revision_id] =&gt; 483
)

stdClass Object
(
    [Row] =&gt; 1
    [Type] =&gt; M
    [Prefix] =&gt; Miss
    [FirstName] =&gt; [actual last name]
    [MiddleName] =&gt; 
    [LastName] =&gt; [actual first name]
    [StaffNotes] =&gt; 
    [ColumbiaAffiliation] =&gt; Alumna/nus
    [ProfessionalAffiliation] =&gt; 
    [AddressLine1] =&gt; [actual address]
    [AddressLine2] =&gt; 
    [City] =&gt; New York
    [State] =&gt; NY
    [ZipCode] =&gt; 10028
    [Country] =&gt; Array
        (
            [0] =&gt; US
        )

    [TelephoneNumber] =&gt; [actual phone number]
    [Email] =&gt; [actual email]
    [Birthday] =&gt; 
    [ID] =&gt; [actual ID]
    [Descriptor] =&gt; [A] A: EN07
    [GraduationYear] =&gt; 2007
    [SchoolAffiliation] =&gt; EN
    [csvrownum] =&gt; 1
    [migrate_map_hash] =&gt; 
)


my field mappings:

$this-&gt;addFieldMapping('field_user_address', 'Country');    
$this-&gt;addFieldMapping('field_user_address:administrative_area', 'State');
$this-&gt;addFieldMapping('field_user_address:locality', 'City');
$this-&gt;addFieldMapping('field_user_address:postal_code', 'ZipCode');
$this-&gt;addFieldMapping('field_user_address:thoroughfare', 'AddressLine1');
$this-&gt;addFieldMapping('field_user_address:premise', 'AddressLine2');
$this-&gt;addFieldMapping('field_user_address:first_name', 'FirstName');
$this-&gt;addFieldMapping('field_user_address:last_name', 'LastName');


and in prepareRow():

$row-&gt;Country = array($row-&gt;Country);
I gave up with the Addressfield Migration Class and decided to do a db_insert -- and it worked! 

Not only works, but because I register the insert in the prepare() function of class SchoolMigration (where I was already using db queries) it also rolls back!

&lt;?php
/**
 * @file migrate_users.migrate.inc
 * Our own hook implementation.
 */

/**
 * Migration classes for migrating users and user fields
 *
 * see Migrate handbook: https://drupal.org/migrate
 * and http://www.verbosity.ca/working-drupals-migrate-module
*/

/* useful functions for debugging using drush migrate commands:
drush_print_r($row);
drush_print_r($this-&gt;destinationValues);
*/    

/**
 * Implements hook_migrate_api()
 *
 * Returns 'api' =&gt; 2 for the 7.x-2.x branch of Migrate.
 * Registers the migration classes for the 7.x-2.6 branch of Migrate (including
 * 7.x-2.6+xx-dev).
 */

function migrate_users_migrate_api() {
  $api = array(
    'api' =&gt; 2,

    // Migrations can be organized into groups. The key used here will be the
    // machine name of the group, which can be used in Drush:
    //  drush migrate-import --group=MigrateMembers
    // The title is a required argument which is displayed for the group in the
    // UI. You may also have additional arguments for any other data which is
    // common to all migrations in the group.
    'groups' =&gt; array(
      'MigrateMembersGroup' =&gt; array(
        'title' =&gt; t('Import Member Users'),
      ),
    ),

    // Here we register the individual migrations. The keys (MigrateMembers_Step1,
    // etc.) are the machine names of the migrations, and the class_name
    // argument is required. The group_name is optional (defaulting to 'default')
    // but specifying it is a best practice.
    'migrations' =&gt; array(
      'MigrateMembers' =&gt; array(
        'class_name' =&gt; 'MigrateMembers',
        'group_name' =&gt; 'MigrateMembersGroup',
      ),
      'GraduationYear' =&gt; array(
        'class_name' =&gt; 'GraduationYearMigration',
        'group_name' =&gt; 'MigrateMembersGroup',
      ),
      'School' =&gt; array(
        'class_name' =&gt; 'SchoolMigration',
        'group_name' =&gt; 'MigrateMembersGroup',
      ),
      'SchoolID' =&gt; array(
        'class_name' =&gt; 'SchoolIDMigration',
        'group_name' =&gt; 'MigrateMembersGroup',
      ),
    ),
  );
  return $api;
}

/**
* Abstract class as a base for all our migration classes
*/

abstract class MigrateMembers_Basic_Migration extends Migration {
  public function __construct($arguments) {
    // Always call the parent constructor first for basic setup
    parent::__construct($arguments);
    // Avoid known line ending issue: drupal.org/node/1705850
    ini_set('auto_detect_line_endings', TRUE);
  }
}


/**
* User-only migration 
* (and creates $row (Row) with user id to key other tables in subsequent migration classes)
*
* The data file is assumed to be in
* sites/all/modules/migrate_users/data_sources/
* the columns in the csv must be in the same order left-to-right as the $columns array below
* every row in csv must have a unique id in the first column (can be ascending numbers, for example)
*/
class MigrateMembers extends MigrateMembers_Basic_Migration {

public function __construct($arguments) {
  parent::__construct($arguments);
  $this-&gt;description = t('Import an CSV-file (only "User Account"-fields)');
  $columns = array(
  // Format: ('Fieldname', t('Description'))
      0 =&gt; array('Row', t('Row number (must be unique)')),
      1 =&gt; array('Type', t('S = Student M = Member')),
      2 =&gt; array('Prefix', t('Mr. Mrs. etc')),
      3 =&gt; array('FirstName', t('First Name')),
      4 =&gt; array('MiddleName', t('MiddleName')),
      5 =&gt; array('LastName', t('Last Name')),
      6 =&gt; array('StaffNotes', t('Staff Notes')),
      7 =&gt; array('SchoolNameAffiliation', t('SchoolName Affiliation')),
      8 =&gt; array('ProfessionalAffiliation', t('Professional Affiliation ')),
      9 =&gt; array('AddressLine1', t('Address Line 1')),
      10 =&gt; array('AddressLine2', t('Address Line 2')),
      11 =&gt; array('City', t('City')),
      12 =&gt; array('State', t('State')),
      13 =&gt; array('ZipCode', t('Zip Code')),
      14 =&gt; array('Country', t('Country')),
      15 =&gt; array('TelephoneNumber', t('Telephone Number')),
      16 =&gt; array('EMAIL', t('Email')),
      17 =&gt; array('Birthday', t('Birthday')),
      18 =&gt; array('SchoolID', t('SchoolID')),
      19 =&gt; array('Descriptor', t('Descriptor')),
      20 =&gt; array('GraduationYear', t('Graduation Year')),
      21 =&gt; array('SchoolAffiliation', t('SchoolAffiliation')),
  );

  // TIP: delete ", array('header_rows' =&gt; 1)" in the next line, if the CSV-file has NO header-line
  $this-&gt;source = new MigrateSourceCSV(DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_users') . '/data_sources/drupaluser_import.csv', $columns, array('header_rows' =&gt; 1));
  $this-&gt;destination = new MigrateDestinationUser();
  $this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
  array('Row' =&gt; array( // this field is used to connect user to other tables
          'type' =&gt; 'varchar',
          'length' =&gt; 6,
          'not null' =&gt; TRUE,
          'description' =&gt; t('User\'s Member-ID') // description never used
        )
       ),
  MigrateDestinationUser::getKeySchema()
  );

  // Mapped fields

  $this-&gt;addFieldMapping('name', 'EMAIL') 
    -&gt;defaultValue('')
    -&gt;description(t('Name (User Account)'));
  $this-&gt;addFieldMapping('mail', 'EMAIL') 
    -&gt;defaultValue('')
    -&gt;description(t('Email address'));
  $this-&gt;addFieldMapping('init')
    -&gt;defaultValue('')
    -&gt;description(t('Email address used for initial user-account creation'));
  $this-&gt;addFieldMapping('pass', 'PASSWORD')
    -&gt;defaultValue('asdf')
    -&gt;description(t("User's password (plain text)"));
  $this-&gt;addFieldMapping('is_new')
    -&gt;defaultValue(TRUE)
    -&gt;description(t('Build the new user (0|1)'));
  $this-&gt;addFieldMapping('roles')
    -&gt;defaultValue(6) 
    -&gt;description(6 . t(' = "member"'));
  $this-&gt;addFieldMapping('theme')
    -&gt;defaultValue('')
    -&gt;description(t("User's default theme"));
  $this-&gt;addFieldMapping('signature')
    -&gt;defaultValue('')
    -&gt;description(t("User's signature"));
  $this-&gt;addFieldMapping('signature_format')
    -&gt;defaultValue('filtered_html')
    -&gt;description(t('Which filter applies to this signature'));
  $this-&gt;addFieldMapping('created')
    -&gt;defaultValue(time())
    -&gt;description(t('unique timestamp of user creation date'));
  $this-&gt;addFieldMapping('access')
    -&gt;defaultValue(0)
    -&gt;description(t('unique timestamp for previous time user accessed the site'));
  $this-&gt;addFieldMapping('login')
    -&gt;defaultValue(0)
    -&gt;description(t('unique timestamp for user\'s last login'));
  $this-&gt;addFieldMapping('status')
    -&gt;defaultValue(1)
    -&gt;description(t('Whether the user is active(1) or blocked(0)'));
  $this-&gt;addFieldMapping('timezone')
    -&gt;defaultValue(t('Europe/London')) // 'America/Los_Angeles', 'Europe/Berlin', 'UTC', ... from drupal.org/node/714214
    -&gt;description(t("User's time zone"));
  $this-&gt;addFieldMapping('language')
    -&gt;defaultValue(t('en')) // e.g.: 'en', 'fr', 'de', ...
    -&gt;description(t("User's default language"));
  $this-&gt;addFieldMapping('picture')
    -&gt;defaultValue(0)  
    -&gt;description(t('Avatar of the user'));


  // Other handlers
  if (module_exists('path')) {
    $this-&gt;addFieldMapping('path')
       -&gt;defaultValue(NULL)
       -&gt;description(t('Path alias'));
  }
  if (module_exists('pathauto')) {
    $this-&gt;addFieldMapping('pathauto')
    -&gt;defaultValue(1) 
    -&gt;description(t('Perform aliasing (set to 0 to prevent alias generation during migration)'));
  }
  // Unmapped destination fields
  $this-&gt;addUnmigratedDestinations(array('role_names', 'data'));
  }  
}

class GraduationYearMigration extends MigrateMembers_Basic_Migration {
  public function __construct($arguments) {
  parent::__construct($arguments);
  $this-&gt;description = 'Add data to field_data_field_graduation_year';
  $this-&gt;dependencies = array('MigrateMembers');
  $table_name = 'field_data_field_graduation_year';

  $this-&gt;source = new MigrateSourceCSV(DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_users') . '/data_sources/drupaluser_import.csv', $columns, array('header_rows' =&gt; 1));
  $this-&gt;destination = new MigrateDestinationTable($table_name);
  $this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
    array('Row' =&gt; array(
      'type' =&gt; 'int',
      'unsigned' =&gt; TRUE,
      'not null' =&gt; TRUE,
       )
     ),
    MigrateDestinationTable::getKeySchema($table_name)
    );

  // Mapped fields
  // add uid from completed MigrateMembers migration
  $this-&gt;addFieldMapping('entity_id', 'Row')-&gt;sourceMigration('MigrateMembers');
  $this-&gt;addFieldMapping('revision_id', 'Row')-&gt;sourceMigration('MigrateMembers');

  // Insert Graduation Year value
  $this-&gt;addFieldMapping('field_graduation_year_value', 'SchoolAffiliation')-&gt;sourceMigration('MigrateMembers')
    -&gt;defaultValue(date("Y"));

  // set some defaults from the schema
  $this-&gt;addFieldMapping('delta')
    -&gt;defaultValue("0");
  $this-&gt;addFieldMapping('entity_type')
    -&gt;defaultValue("user");
  $this-&gt;addFieldMapping('bundle')
    -&gt;defaultValue("user");
  $this-&gt;addFieldMapping('language')
    -&gt;defaultValue("und");

  $this-&gt;removeFieldMapping('path');
  $this-&gt;removeFieldMapping('pathauto');
  }
}

class SchoolMigration extends MigrateMembers_Basic_Migration {
  public function __construct($arguments) {
    parent::__construct($arguments);
    $this-&gt;description = 'Add data to field_data_field_member_school';

    $this-&gt;dependencies = array('MigrateMembers');
    $table_name = 'field_data_field_member_school';
    $this-&gt;source = new MigrateSourceCSV(DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_users') . '/data_sources/drupaluser_import.csv', $columns, array('header_rows' =&gt; 1));
    $this-&gt;destination = new MigrateDestinationTable($table_name);
    $this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
      array('Row' =&gt; array(
        'type' =&gt; 'int',
        'unsigned' =&gt; TRUE,
        'not null' =&gt; TRUE,
         )
       ),
      MigrateDestinationTable::getKeySchema($table_name)
      );

    $this-&gt;addFieldMapping('entity_id', 'Row')-&gt;sourceMigration('MigrateMembers');
    $this-&gt;addFieldMapping('revision_id', 'Row')-&gt;sourceMigration('MigrateMembers');
    $this-&gt;addFieldMapping('field_member_school_tid', 'SchoolAffiliation')-&gt;sourceMigration('MigrateMembers');


    // set some defaults from the schema
    $this-&gt;addFieldMapping('delta')
      -&gt;defaultValue("0");
    $this-&gt;addFieldMapping('entity_type')
      -&gt;defaultValue("user");
    $this-&gt;addFieldMapping('bundle')
      -&gt;defaultValue("user");
    $this-&gt;addFieldMapping('language')
      -&gt;defaultValue("und");

    $this-&gt;removeFieldMapping('path');
    $this-&gt;removeFieldMapping('pathauto');

  }

  public function prepareRow($row){
    // convert 3 letter codes to ISO 3166-1 standard 2 letter codes http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2#Current_codes
    // TODO:
    // 1 - check for full list of country codes in old data
    // 2 - many records with US addresses do not have a foreing country code, not the USA country code
    // 3 - What are INO SAF SWD?
    switch ($row-&gt;Country)
      {
        case 'AUS': // Austria
        $row-&gt;Country = "AT";
        break;
        case 'BEL': // Belgium
        $row-&gt;Country = "BE";
        break;
        [more mappings]
        default : 
        $row-&gt;Country = "US";
        break;      
      }

  }

  function prepare($entity, stdClass $row){

    // delete rows added by MigrateMembers
    $num_deleted1 = db_delete('field_data_field_member_school')
      -&gt;condition('entity_id', $this-&gt;destinationValues-&gt;entity_id)
      -&gt;execute();    
    $num_deleted2 = db_delete('field_data_field_user_address')
      -&gt;condition('entity_id', $this-&gt;destinationValues-&gt;entity_id)
      -&gt;execute();

      switch ($row-&gt;SchoolAffiliation)
      {
        case 'AR': // Arts
        $row-&gt;SchoolAffiliation = "91";
        break;
        [more mappings]
        default : 
        $row-&gt;SchoolAffiliation = "NULL";
        break;      
      }

    $this-&gt;destinationValues-&gt;field_member_school_tid = $row-&gt;SchoolAffiliation;

    $fields = array('entity_type' =&gt; 'user', 'bundle' =&gt; 'user', 'deleted' =&gt; 0, 'entity_id' =&gt; $this-&gt;destinationValues-&gt;entity_id, 'revision_id' =&gt; $this-&gt;destinationValues-&gt;entity_id, 'language' =&gt; 'und', 'delta' =&gt; 0, 'field_user_address_country' =&gt; $row-&gt;Country, 'field_user_address_administrative_area' =&gt; $row-&gt;State, 'field_user_address_sub_administrative_area' =&gt; '', 'field_user_address_locality' =&gt; $row-&gt;City, 'field_user_address_dependent_locality' =&gt; '', 'field_user_address_postal_code' =&gt; $row-&gt;ZipCode, 'field_user_address_thoroughfare' =&gt; $row-&gt;AddressLine1, 'field_user_address_premise' =&gt; $row-&gt;AddressLine2, 'field_user_address_sub_premise' =&gt; '', 'field_user_address_organisation_name' =&gt; '', 'field_user_address_name_line' =&gt; '', 'field_user_address_first_name' =&gt; $row-&gt;FirstName, 'field_user_address_last_name' =&gt; $row-&gt;LastName, 'field_user_address_data' =&gt; '');
    db_insert('field_data_field_user_address')-&gt;fields($fields)-&gt;execute(); 
  }
}

class SchoolIDMigration extends MigrateMembers_Basic_Migration {
  public function __construct($arguments) {
    parent::__construct($arguments);
    $this-&gt;description = 'Add data to field_data_field_SchoolID';
    $this-&gt;dependencies = array('MigrateMembers');
    $table_name = 'field_data_field_SchoolID';

    $this-&gt;source = new MigrateSourceCSV(DRUPAL_ROOT . '/' . drupal_get_path('module', 'migrate_users') . '/data_sources/drupaluser_import.csv', $columns, array('header_rows' =&gt; 1));
    $this-&gt;destination = new MigrateDestinationTable($table_name);
    $this-&gt;map = new MigrateSQLMap($this-&gt;machineName,
      array('Row' =&gt; array(
            'type' =&gt; 'int',
            'unsigned' =&gt; TRUE,
            'not null' =&gt; TRUE,
           )
         ),
        MigrateDestinationTable::getKeySchema($table_name)
      );

    // add uid from completed MigrateMembers migration
    $this-&gt;addFieldMapping('entity_id', 'Row')-&gt;sourceMigration('MigrateMembers');
    $this-&gt;addFieldMapping('revision_id', 'Row')-&gt;sourceMigration('MigrateMembers');

    //this assignment does not find the SchoolID; it is set in prepare()
    //$this-&gt;addFieldMapping('field_SchoolID_value', 'SchoolID')-&gt;sourceMigration('MigrateMembers');

    // set some defaults from the schema
    $this-&gt;addFieldMapping('delta')
      -&gt;defaultValue("0");
    $this-&gt;addFieldMapping('entity_type')
      -&gt;defaultValue("user");
    $this-&gt;addFieldMapping('bundle')
      -&gt;defaultValue("user");
    $this-&gt;addFieldMapping('language')
      -&gt;defaultValue("und");
    $this-&gt;addFieldMapping('field_SchoolID_format')
      -&gt;defaultValue("plain_text");


    $this-&gt;removeFieldMapping('path');
    $this-&gt;removeFieldMapping('pathauto');
  }

  function prepare($entity, stdClass $row){
    // delete rows in table added by MigrateMembers
    $num_deleted = db_delete('field_data_field_SchoolID')
      -&gt;condition('entity_id', $this-&gt;destinationValues-&gt;entity_id)
      -&gt;execute();

    //
    if ($row-&gt;SchoolID) $this-&gt;destinationValues-&gt;field_SchoolID_value = $row-&gt;SchoolID;
    else $this-&gt;destinationValues-&gt;field_SchoolID_value = "NULL";
  }
}