You are here

public function WineWineMigration::__construct in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 migrate_example/wine.inc \WineWineMigration::__construct()

General initialization of a Migration object.

Overrides AdvancedExampleMigration::__construct

File

migrate_example/wine.inc, line 1210
Advanced migration examples. These serve two purposes:

Class

WineWineMigration

Code

public function __construct($arguments) {
  parent::__construct($arguments);
  $this->description = t('Wines of the world');
  $query = db_select('migrate_example_wine', 'w')
    ->fields('w', array(
    'wineid',
    'name',
    'body',
    'excerpt',
    'accountid',
    'posted',
    'last_changed',
    'variety',
    'region',
    'rating',
  ));
  $query
    ->leftJoin('migrate_example_wine_category_wine', 'cwbw', "w.wineid = cwbw.wineid");
  $query
    ->leftJoin('migrate_example_wine_categories', 'bw', "cwbw.categoryid = bw.categoryid AND bw.type = 'best_with'");

  // Gives a single comma-separated list of related terms
  $query
    ->groupBy('w.wineid');
  $query
    ->addExpression('GROUP_CONCAT(bw.categoryid)', 'best_with');
  $count_query = db_select('migrate_example_wine', 'w');
  $count_query
    ->addExpression('COUNT(wineid)', 'cnt');

  // TIP: By passing an array of source fields to the MigrateSourceSQL
  // constructor, we can modify the descriptions of source fields (which just
  // default, for SQL migrations, to table_alias.column_name), as well as add
  // additional fields (which may be populated in prepareRow()).
  $source_fields = array(
    'wineid' => t('Wine ID in the old system'),
    'name' => t('The name of the wine'),
    'best_vintages' => t('What years were best for this wine?'),
    'url' => t('Image URLs attached to this wine; populated in prepareRow()'),
    'image_alt' => t('Image alt text attached to this wine; populated in prepareRow()'),
    'image_title' => t('Image titles attached to this wine; populated in prepareRow()'),
  );

  // TIP: By default, each time a migration is run, any previously unprocessed
  // source items are imported (along with any previously-imported items
  // marked for update). If the source data contains a timestamp that is set
  // to the creation time of each new item, as well as set to the update time
  // for any existing items that are updated, then you can have those updated
  // items automatically reimported by setting the field as your highwater
  // field.
  $this->highwaterField = array(
    'name' => 'last_changed',
    // Column to be used as highwater mark
    'alias' => 'w',
    // Table alias containing that column
    'type' => 'int',
  );

  // Note that it is important to process rows in the order of the highwater
  // mark.
  $query
    ->orderBy('last_changed');
  $this->source = new MigrateSourceSQL($query, $source_fields, $count_query);
  $this->destination = new MigrateDestinationNode('migrate_example_wine');

  // You can add a 'track_last_imported' option to the map, to record the
  // timestamp of when each item was last imported in the map table.
  $this->map = new MigrateSQLMap($this->machineName, array(
    'wineid' => array(
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'description' => 'Wine ID',
      'alias' => 'w',
    ),
  ), MigrateDestinationNode::getKeySchema(), 'default', array(
    'track_last_imported' => TRUE,
  ));

  // Mapped fields
  $this
    ->addFieldMapping('title', 'name')
    ->description(t('Mapping wine name in source to node title'));
  $this
    ->addFieldMapping('uid', 'accountid')
    ->sourceMigration('WineUser')
    ->defaultValue(1);

  // TIP: By default, term relationship are assumed to be passed by name.
  // In this case, the source values are IDs, so we specify the relevant
  // migration (so the tid can be looked up in the map), and tell the term
  // field handler that it is receiving tids instead of names
  $this
    ->addFieldMapping('migrate_example_wine_varieties', 'variety')
    ->sourceMigration('WineVariety');
  $this
    ->addFieldMapping('migrate_example_wine_varieties:source_type')
    ->defaultValue('tid');
  $this
    ->addFieldMapping('migrate_example_wine_regions', 'region')
    ->sourceMigration('WineRegion');
  $this
    ->addFieldMapping('migrate_example_wine_regions:source_type')
    ->defaultValue('tid');
  $this
    ->addFieldMapping('migrate_example_wine_best_with', 'best_with')
    ->separator(',')
    ->sourceMigration('WineBestWith');
  $this
    ->addFieldMapping('migrate_example_wine_best_with:source_type')
    ->defaultValue('tid');
  $this
    ->addFieldMapping('field_migrate_example_wine_ratin', 'rating');
  $this
    ->addFieldMapping('field_migrate_example_top_vintag', 'best_vintages');

  // TIP: You can apply one or more functions to a source value using
  // ->callbacks(). The function must take a single argument and return a
  // value which is a transformation of the argument. As this example shows,
  // you can have multiple callbacks, and they can either be straight
  // functions or class methods. In this case, our custom method prepends
  // 'review: ' to the body, and then we call a standard Drupal function to
  // uppercase the whole body. You can also use ->callback() to pass
  // additional arguments.
  $this
    ->addFieldMapping('body', 'body')
    ->callbacks(array(
    $this,
    'addTitlePrefix',
  ), 'drupal_strtoupper')
    ->callback('html_entity_decode', ENT_QUOTES, 'ISO8859-1');
  $this
    ->addFieldMapping('body:summary', 'excerpt');

  // We will get the image data from a related table in prepareRow()
  $this
    ->addFieldMapping('field_migrate_example_image', 'url');

  // Indicate that we want each file to maintain its name, replacing any
  // previous file of the same name (as opposed to being renamed to avoid
  // collisions, which is the default).
  $this
    ->addFieldMapping('field_migrate_example_image:file_replace')
    ->defaultValue(FILE_EXISTS_REPLACE);
  $this
    ->addFieldMapping('field_migrate_example_image:alt', 'image_alt');
  $this
    ->addFieldMapping('field_migrate_example_image:title', 'image_title');
  $this
    ->addFieldMapping('sticky')
    ->defaultValue(0);
  $this
    ->addFieldMapping('created', 'posted');
  $this
    ->addFieldMapping('changed', 'last_changed');

  // Unmapped source fields
  $this
    ->addUnmigratedSources(array(
    'last_changed',
  ));

  // Unmapped destination fields
  $this
    ->addUnmigratedDestinations(array(
    'body:format',
    'comment',
    'field_migrate_example_image:destination_dir',
    'field_migrate_example_image:destination_file',
    'field_migrate_example_image:file_class',
    'field_migrate_example_image:preserve_files',
    'field_migrate_example_image:source_dir',
    'field_migrate_example_image:urlencode',
    'is_new',
    'language',
    'log',
    'migrate_example_wine_best_with:create_term',
    'migrate_example_wine_best_with:ignore_case',
    'migrate_example_wine_regions:create_term',
    'migrate_example_wine_regions:ignore_case',
    'migrate_example_wine_varieties:create_term',
    'migrate_example_wine_varieties:ignore_case',
    'promote',
    'revision',
    'revision_uid',
    'status',
    'tnid',
    'translate',
  ));
  if (module_exists('statistics')) {
    $this
      ->addUnmigratedDestinations(array(
      'totalcount',
      'daycount',
      'timestamp',
    ));
  }
}