You are here

public function WineWineMigration::__construct in Migrate 6.2

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

General initialization of a Migration object.

Overrides AdvancedExampleMigration::__construct

File

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

Class

WineWineMigration

Code

public function __construct() {
  parent::__construct();
  $this->description = t('Wines of the world');
  $this->dependencies = array(
    'WineVariety',
    'WineRegion',
    'WineBestWith',
    'WineUser',
    'WineProducer',
  );

  // 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,
  ));
  $query = db_select('migrate_example_wine', 'w')
    ->fields('w', array(
    'wineid',
    'name',
    'body',
    'excerpt',
    'accountid',
    'posted',
    'last_changed',
    'variety',
    'region',
    'rating',
    'last_reviewed',
  ));
  $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?'),
    'images' => t('Images attached to this wine; populated in prepareRow()'),
  );

  // TIP: By default, each time a migration is run, any previously unimported 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');

  // 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')
    ->arguments(array(
    'source_type' => 'tid',
  ));
  $this
    ->addFieldMapping('Migrate Example Wine Regions', 'region')
    ->sourceMigration('WineRegion')
    ->arguments(array(
    'source_type' => 'tid',
  ));
  $this
    ->addFieldMapping('Migrate Example Wine Best With', 'best_with')
    ->separator(',')
    ->sourceMigration('WineBestWith')
    ->arguments(array(
    'source_type' => 'tid',
  ));
  $this
    ->addFieldMapping('field_migrate_example_wine_ratin', 'rating');
  $this
    ->addFieldMapping('field_migrate_example_wine_rvw', 'last_reviewed');
  $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.
  $this
    ->addFieldMapping('body', 'body')
    ->callbacks(array(
    $this,
    'addTitlePrefix',
  ), 'drupal_strtoupper');
  $this
    ->addFieldMapping('teaser', 'excerpt');

  // We will get the image data from a related table in prepareRow()
  $arguments = MigrateFileFieldHandler::arguments(NULL, 'file_copy', FILE_EXISTS_REPLACE);
  $this
    ->addFieldMapping('field_migrate_example_image', 'images')
    ->arguments($arguments);
  $this
    ->addFieldMapping('sticky')
    ->defaultValue(0);

  // These are already UNIX timestamps, so just pass through
  $this
    ->addFieldMapping('created', 'posted');
  $this
    ->addFieldMapping('changed', 'last_changed');

  // No unmapped source fields
  // Unmapped destination fields
  $this
    ->addUnmigratedDestinations(array(
    'is_new',
    'status',
    'promote',
    'revision',
    'language',
  ));
}