table_copy.inc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. /**
  3. * @file
  4. * Copies data_row into a table using drupal_write_record()
  5. */
  6. /**
  7. * Destination class implementing migration into a single table.
  8. */
  9. class MigrateDestinationTableCopy extends MigrateDestination {
  10. public function __construct($tableName, $keySchema) {
  11. parent::__construct();
  12. $this->tableName = $tableName;
  13. $this->keySchema = $keySchema;
  14. }
  15. public function __toString() {
  16. $output = t('Table copy');
  17. return $output;
  18. }
  19. /**
  20. * Delete a batch of rows at once.
  21. *
  22. * @param $ids
  23. * Array of IDs to be deleted.
  24. */
  25. public function bulkRollback(array $ids) {
  26. migrate_instrument_start('table_copy bulkRollback');
  27. db_delete($this->tableName)
  28. ->condition(key($this->keySchema), $ids, 'IN')
  29. ->execute();
  30. migrate_instrument_stop('table_copy bulkRollback');
  31. }
  32. /**
  33. * Import a single row.
  34. *
  35. * @param $entity
  36. * Object object to build. Prefilled with any fields mapped in the Migration.
  37. * @param $row
  38. * Raw source data object - passed through to prepare/complete handlers.
  39. * @return array
  40. * Array of key fields of the object that was saved if
  41. * successful. FALSE on failure.
  42. */
  43. public function import(stdClass $entity, stdClass $row) {
  44. $migration = MigrationBase::currentMigration();
  45. $fields = clone $row;
  46. $keys = array_keys($this->keySchema);
  47. $values = array();
  48. foreach ($keys as $key) {
  49. $values[] = $row->$key;
  50. }
  51. unset($fields->migrate_map_destid1);
  52. unset($fields->needs_update);
  53. $query = db_merge($this->tableName)->key($keys, $values)->fields((array)$fields);
  54. try {
  55. $status = $query->execute();
  56. if ($status == MergeQuery::STATUS_INSERT) {
  57. $this->numCreated++;
  58. }
  59. else {
  60. $this->numUpdated++;
  61. }
  62. return $values;
  63. }
  64. catch (MigrateException $e) {
  65. $migration->saveMessage($e->getMessage(), $e->getLevel());
  66. Migration::displayMessage($e->getMessage());
  67. }
  68. catch (Exception $e) {
  69. $this->handleException($e);
  70. }
  71. }
  72. public function fields($migration = NULL) {
  73. return array();
  74. }
  75. }