You are here

public function MigrateSourceSQL::fields in Migrate 7.2

Same name and namespace in other branches
  1. 6.2 plugins/sources/sql.inc \MigrateSourceSQL::fields()

Returns a list of fields available to be mapped from the source query.

Return value

array Keys: machine names of the fields (to be passed to addFieldMapping) Values: Human-friendly descriptions of the fields.

Overrides MigrateSource::fields

File

plugins/sources/sql.inc, line 204
Define a MigrateSource for importing from Drupal connections.

Class

MigrateSourceSQL
Implementation of MigrateSource, to handle imports from Drupal connections.

Code

public function fields() {
  $fields = array();
  $queryFields = $this->query
    ->getFields();
  if ($queryFields) {

    // Not much we can do in terms of describing the fields
    // without manual intervention.
    foreach ($queryFields as $field_name => $field_info) {
      $fields[$field_name] = $field_info['table'] . '.' . $field_info['field'];
    }
  }
  else {

    // Detect available fields.
    $detection_query = clone $this->query;
    $result = $detection_query
      ->range(0, 1)
      ->execute();
    $row = $result
      ->fetchAssoc();
    if (is_array($row)) {
      foreach ($row as $field_name => $field_value) {
        $fields[$field_name] = t('Example Content: !value', array(
          '!value' => $field_value,
        ));
      }
    }
  }

  /*
       * Handle queries without explicit field lists
       * TODO: Waiting on http://drupal.org/node/814312
          $info = Database::getConnectionInfo($query->getConnection());
          $database = $info['default']['database'];
          foreach ($this->query->getTables() as $table) {
            if (isset($table['all_fields']) && $table['all_fields']) {

              $database = 'plants';
              $table = $table['table'];
              $sql = 'SELECT column_name
                      FROM information_schema.columns
                      WHERE table_schema=:database AND table_name = :table
                      ORDER BY ordinal_position';
              $result = dbtng_query($sql, array(':database' => $database, ':table' => $table));
              foreach ($result as $row) {
                $fields[$row->column_name] = $table . '.' . $row->column_name;
              }
            }
          }*/
  $expressionFields = $this->query
    ->getExpressions();
  foreach ($expressionFields as $field_name => $field_info) {
    $fields[$field_name] = $field_info['alias'];
  }

  // Any caller-specified fields with the same names as extracted fields will
  // override them; any others will be added
  if ($this->fields) {
    $fields = $this->fields + $fields;
  }
  return $fields;
}