Created
June 7, 2012 23:02
-
-
Save btmash/2892242 to your computer and use it in GitHub Desktop.
DE Migration Example SOURCE::DESTINATION code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class DEMigrationCommerceProductMigration extends Migration { | |
protected $file_public_path = ""; | |
protected $source_drupal_root = ""; | |
public function __construct() { | |
parent::__construct(); | |
$this->systemOfRecord = Migration::DESTINATION; | |
$this->description = t('Update fields for commerce products'); | |
$arguments = array( | |
'type' => 'product', | |
); | |
$this->dependencies = array('CommerceMigrateUbercartProductProduct'); | |
$this->file_public_path = variable_get('commerce_migrate_ubercart_public_files_directory', variable_get('file_public_path', 'sites/default/files')); | |
$this->source_drupal_root = variable_get('commerce_migrate_ubercart_source_drupal_root', DRUPAL_ROOT); | |
// Create a map object for tracking the relationships between source rows | |
$this->map = new MigrateSQLMap($this->machineName, | |
array( | |
'nid' => array( | |
'type' => 'int', | |
'unsigned' => TRUE, | |
'not null' => TRUE, | |
'description' => 'Ubercart node ID', | |
'alias' => 'ucp', | |
), | |
), | |
MigrateDestinationEntityAPI::getKeySchema('commerce_product', $arguments['type']) | |
); | |
// Create a MigrateSource object, which manages retrieving the input data. | |
$connection = commerce_migrate_ubercart_get_source_connection(); | |
$query = $connection->select('node', 'n'); | |
$query->innerJoin('content_type_product', 'ctp', 'n.nid = ctp.nid and n.vid = ctp.vid'); | |
$query->innerJoin('uc_products', 'ucp', 'n.nid = ucp.nid AND n.vid = ucp.vid'); | |
$query->fields('n', array('nid', 'vid')) | |
->fields('ctp', array('field_award_winner_value', 'field_lp_short_desc_value', 'field_lp_image_fid', 'field_lp_image_data', 'field_lp2_short_desc_value', 'field_lp2_image_fid', 'field_lp2_image_data', 'field_beauty_award_value', 'field_awards_lp_image_fid', 'field_awards_lp_image_data')) | |
->fields('ucp', array('model', 'sell_price')) | |
->condition('n.type', $arguments['type']) | |
->distinct(); | |
$this->source = new MigrateSourceSQL($query, array(), NULL, array('map_joinable' => FALSE)); | |
$this->destination = new MigrateDestinationEntityAPI('commerce_product', $arguments['type']); | |
$this->addFieldMapping('product_id', 'nid')->sourceMigration('CommerceMigrateUbercartProductProduct'); | |
$this->addFieldMapping('commerce_stock', 'stock'); | |
// TEXT FIELDS | |
$generic_textarea_arguments = MigrateTextFieldHandler::arguments(NULL, 'full_html'); | |
$this->addFieldMapping('field_award_winner', 'field_award_winner_value') | |
->arguments($generic_textarea_arguments); | |
$this->addFieldMapping('field_lp_short_desc', 'field_lp_short_desc_value') | |
->arguments($generic_textarea_arguments); | |
$this->addFieldMapping('field_lp2_short_desc', 'field_lp2_short_desc_value') | |
->arguments($generic_textarea_arguments); | |
$this->addFieldMapping('field_beauty_award_desc', 'field_beauty_award_value') | |
->arguments($generic_textarea_arguments); | |
$generic_image_arguments = array( | |
'file_function' => 'file_copy', | |
'file_replace' => FILE_EXISTS_RENAME, | |
); | |
$this->addFieldMapping('field_lp_image', 'lp_image') | |
->arguments($generic_image_arguments); | |
$this->addFieldMapping('field_lp2_image', 'lp2_image') | |
->arguments($generic_image_arguments); | |
$this->addFieldMapping('field_awards_lp_image', 'awards_lp_image') | |
->arguments($generic_image_arguments); | |
// All the items that do not require mappings | |
$this->addFieldMapping('sku')->issueGroup(t('DNM')); | |
$this->addFieldMapping('type')->issueGroup(t('DNM')); | |
$this->addFieldMapping('title')->issueGroup(t('DNM')); | |
$this->addFieldMapping('status')->issueGroup(t('DNM')); | |
$this->addFieldMapping('created')->issueGroup(t('DNM')); | |
$this->addFieldMapping('changed')->issueGroup(t('DNM')); | |
$this->addFieldMapping('uid')->issueGroup(t('DNM')); | |
$this->addFieldMapping('commerce_price')->issueGroup(t('DNM')); | |
$this->addFieldMapping('field_image')->issueGroup(t('DNM')); | |
$this->addFieldMapping('path')->issueGroup(t('DNM')); | |
$this->addUnmigratedSources(array('vid', 'model', 'sell_price', 'field_lp_image_fid', 'field_lp_image_data', 'field_lp2_image_fid', 'field_lp2_image_data', 'field_awards_lp_image_fid', 'field_awards_lp_image_data')); | |
} | |
public function prepareRow($current_row) { | |
// Load the current product | |
$results = db_query("SELECT destid1 FROM {migrate_map_commercemigrateubercartproductproduct} WHERE sourceid1 = :source_id", array(':source_id' => $current_row->nid)); | |
foreach ($results as $result) { | |
$current_row->pid = $result->destid1; | |
$product = commerce_product_load($current_row->pid); | |
$current_row->product = clone $product; | |
} | |
// Figure out stock. | |
$connection = commerce_migrate_ubercart_get_source_connection(); | |
$current_row->stock = 0; | |
$results = $connection->query("SELECT * FROM {uc_product_stock} WHERE nid=:nid", array(':nid' => $current_row->nid)); | |
foreach ($results as $result) { | |
$current_row->stock = $result->stock; | |
} | |
// Load images; | |
$current_row->lp_image = $this->generateMigrateFile($current_row->field_lp_image_fid, $current_row->field_lp_image_data); | |
$current_row->lp2_image = $this->generateMigrateFile($current_row->field_lp2_image_fid, $current_row->field_lp2_image_data); | |
$current_row->awards_lp_image = $this->generateMigrateFile($current_row->field_awards_lp_image_fid, $current_row->field_awards_lp_image_data); | |
// $this->generateImageMigration($current_row); | |
// If, for some reason, you need to not import particular content, return FALSE | |
// return FALSE; | |
} | |
public function prepare($entity, stdClass $current_row) { | |
$product = $current_row->product; | |
$entity->commerce_price = $product->commerce_price; | |
$entity->field_image = $product->field_image; | |
} | |
private function generateMigrateFile($fid = NULL, $file_data = '') { | |
$files = array(); | |
if (!empty($fid)) { | |
$connection = commerce_migrate_ubercart_get_source_connection(); | |
$query = $connection->select('files', 'f'); | |
$query->fields('f', array('fid', 'filepath')) | |
->condition('fid', $fid); | |
$results = $query->execute(); | |
foreach($results as $record) { | |
$value = unserialize($file_data); | |
$value['path'] = $this->source_drupal_root .'/'. $record->filepath; | |
$files[] = drupal_json_encode($value); | |
} | |
} | |
return $files; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment