You are here

class MigrateDestinationTableCopy in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/destinations/table_copy.inc \MigrateDestinationTableCopy

Destination class implementing migration into a single table.

Hierarchy

Expanded class hierarchy of MigrateDestinationTableCopy

File

plugins/destinations/table_copy.inc, line 11
Copies data_row into a table using drupal_write_record()

View source
class MigrateDestinationTableCopy extends MigrateDestination {
  public function __construct($tableName, $keySchema) {
    parent::__construct();
    $this->tableName = $tableName;
    $this->keySchema = $keySchema;
  }
  public function __toString() {
    $output = t('Table copy');
    return $output;
  }

  /**
   * Delete a batch of rows at once.
   *
   * @param $ids
   *  Array of IDs to be deleted.
   */
  public function bulkRollback(array $ids) {
    migrate_instrument_start('table_copy bulkRollback');
    db_delete($this->tableName)
      ->condition(key($this->keySchema), $ids, 'IN')
      ->execute();
    migrate_instrument_stop('table_copy bulkRollback');
  }

  /**
   * Import a single row.
   *
   * @param $entity
   *  Object object to build. Prefilled with any fields mapped in the Migration.
   * @param $row
   *  Raw source data object - passed through to prepare/complete handlers.
   *
   * @return array
   *  Array of key fields of the object that was saved if
   *  successful. FALSE on failure.
   */
  public function import(stdClass $entity, stdClass $row) {
    $migration = MigrationBase::currentMigration();
    $fields = clone $row;

    // Remove all map data, otherwise we'll try to write it to the destination
    // table.
    foreach ($fields as $field => $data) {
      if (strpos($field, 'migrate_map_') === 0) {
        unset($fields->{$field});
      }
    }
    $keys = array_keys($this->keySchema);
    $values = array();
    foreach ($keys as $key) {
      $values[] = $row->{$key};
    }
    $query = db_merge($this->tableName)
      ->key($keys, $values)
      ->fields((array) $fields);
    try {
      $status = $query
        ->execute();
      if ($status == MergeQuery::STATUS_INSERT) {
        $this->numCreated++;
      }
      else {
        $this->numUpdated++;
      }
      return $values;
    } catch (MigrateException $e) {
      $migration
        ->saveMessage($e
        ->getMessage(), $e
        ->getLevel());
      Migration::displayMessage($e
        ->getMessage());
    } catch (Exception $e) {
      $migration
        ->handleException($e);
    }
  }
  public function fields($migration = NULL) {
    return array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateDestination::$numCreated protected property Maintain stats on the number of destination objects created or updated.
MigrateDestination::$numUpdated protected property
MigrateDestination::getCreated public function
MigrateDestination::getUpdated public function
MigrateDestination::resetStats public function Reset numCreated and numUpdated back to 0.
MigrateDestinationTableCopy::bulkRollback public function Delete a batch of rows at once.
MigrateDestinationTableCopy::fields public function Derived classes must implement fields(), returning a list of available destination fields. Overrides MigrateDestination::fields
MigrateDestinationTableCopy::import public function Import a single row. Overrides MigrateDestination::import
MigrateDestinationTableCopy::__construct public function Null constructor Overrides MigrateDestination::__construct
MigrateDestinationTableCopy::__toString public function Derived classes must implement __toString(). Overrides MigrateDestination::__toString