schema = drupal_get_schema($table_name); $this->tableName = $tableName; } static public function getKeySchema($table_name) { $schema = drupal_get_schema($table_name); $keys = array(); foreach ($schema['primary key'] as $primary_key) { $keys[$primary_key] = $this->schema[$primary_key]; } return $keys; } public function __toString() { $output = t('Table'); return $output; } /** * Delete a single row. * * @param $id * Primary key(s) and their values. */ public function rollback(array $id) { migrate_instrument_start('table bulkRollback'); $delete = db_delete($this->tableName); foreach ($id as $key => $value) { $delete->condition($key, $value); } $delete->execute(); migrate_instrument_stop('table 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) { // Only filled when doing an update. $primary_keys = array(); $migration = Migration::currentMigration(); // Updating previously-migrated content? if (isset($row->migrate_map_destid1)) { $i = 1; foreach ($this->schema['primary key'] as $key) { $destination_id = $row->{'migrate_map_destid' . $i}; if (isset($entity->{$key})) { if ($entity->{$key} != $destination_id) { throw new MigrateException(t("Incoming id !id and map destination id !destid don't match", array('!id' => $entity->{$key}, '!destid' => $destination_id))); } } else { $entity->{$key} = $destination_id; } $i++; } } if ($migration->getSystemOfRecord() == Migration::DESTINATION) { foreach ($this->schema['primary key'] as $key) { if (!isset($entity->{$key})) { throw new MigrateException(t('System-of-record is DESTINATION, but no destination id provided')); } } $select = db_select($this->tableName); foreach ($this->schema['primary key'] as $key) { $select->condition($key, $entity->{$key}); } $old_entity = $select->execute()->fetchObject; foreach ($entity as $field => $value) { $old_entity->$field = $entity->$field; } $entity = $old_entity; } $this->prepare($entity, $row); if (isset($entity->timestamp)) { $entity->timestamp = Migration::timestamp($entity->timestamp); } $status = drupal_write_record($this->tableName, $entity, $primary_keys); $this->complete($entity, $row); if ($status) { return array($entity->vote_id); } } /** * Returns a list of fields available to be mapped/ * * @return array * Keys: machine names of the fields (to be passed to addFieldMapping) * Values: Human-friendly descriptions of the fields. */ public function fields() { $fields = array(); foreach ($this->schema['fields'] as $column => $schema) { $fields[$column] = $schema['description']; } return $fields; } }