Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class PRMigration extends Migration {
- public function __construct() {
- parent::__construct();
- $this->map = new MigrateSQLMap($this->machineName,
- array(
- 'id' => array(
- 'type' => 'int',
- 'not null' => TRUE,
- 'description' => 'Press Release ID',
- )
- ),
- MigrateDestinationNode::getKeySchema()
- );
- $query = Database::getConnection('default', 'secondary')
- ->select('releases', 'r')
- ->fields('r', array('id', 'title', 'released_at', 'synopsis', 'body',
- 'never_post', 'position', 'writer', 'created_by', 'updated_by',
- 'created_at', 'updated_at', 'subtitle', 'contact_id',
- 'version', 'deleted_at'));
- $source_fields = array(
- 'links' => t('Related links'),
- 'source_images' => t('Image assets attached ot this press release.'),
- );
- $this->source = new MigrateSourceSQL($query, $source_fields, NULL, array('map_joinable' => FALSE));
- // Using the default Node destination class.
- $this->destination = new MigrateDestinationNode('press_release');
- ///////////////////////////////////////////////////////
- // Mapped fields
- ///////////////////////////////////////////////////////
- $file_args = MigrateFileFieldHandler::arguments(NULL, 'file_blob', FILE_EXISTS_RENAME);
- $this->addFieldMapping('field_image', 'source_images')
- ->arguments($file_args);
- }
- public function prepareRow($row) {
- // Pull in the associated images for this release.
- $source_db = Database::getConnection('default', 'secondary');
- $query = $source_db
- ->select('assets', 'a')
- ->fields('a', array('id', 'filename', 'content_type', 'alt', 'caption',
- 'photo_credit', 'size', 'height', 'width', 'parent_id', 'thumbnail',
- 'db_file_id', 'created_by', 'updated_by', 'created_at', 'updated_at'));
- $query->innerJoin('db_files', 'dbf', 'a.db_file_id = dbf.id');
- $query->addField('dbf', 'data');
- $query->leftJoin('assignments', 'assign', 'a.id = assign.asset_id');
- $query->condition('assign.category_type', 'Release', '=');
- $query->condition('assign.category_id', $row->id, '=');
- $results = $query->execute();
- $images = array();
- foreach ($results as $row) {
- $image_information = array(
- 'path' => $row->filename,
- 'alt' => $row->alt,
- 'title' => $row->caption,
- );
- $images[] = drupal_json_encode($image_information);
- }
- $row->source_images = $images;
- return TRUE;
- }
- }
- class PRAssetMigration extends Migration {
- public function __construct() {
- parent::__construct();
- $this->map = new MigrateSQLMap($this->machineName,
- array(
- 'id' => array(
- 'type' => 'int',
- 'not null' => TRUE,
- 'description' => 'Asset ID',
- )
- ),
- MigrateDestinationNode::getKeySchema()
- );
- // Select all the fields from the 'assets' table, and join it with the related 'data' field in db_files.
- $source_db = Database::getConnection('default', 'secondary');
- $query = $source_db
- ->select('assets', 'a')
- ->fields('a', array('id', 'filename', 'content_type', 'alt', 'caption',
- 'photo_credit', 'size', 'height', 'width', 'parent_id', 'thumbnail',
- 'db_file_id', 'created_by', 'updated_by', 'created_at', 'updated_at'));
- $query->innerJoin('db_files', 'dbf', 'a.db_file_id = dbf.id');
- $query->addField('dbf', 'data');
- $count_query = $source_db->select('assets', 'a');
- $count_query->addExpression('COUNT(id)', 'cnt');
- // User the simplified count query so it doesn't have to do the join to get the count.
- $this->source = new MigrateSourceSQL($query, array(), $count_query, array('map_joinable' => FALSE));
- $this->destination = new MigrateDestinationFile(MigrateDestinationFile::options(FALSE, NULL, NULL, NULL, FALSE, TRUE));
- ///////////////////////////////////////////////////////
- // Mapped fields
- ///////////////////////////////////////////////////////
- $this->addSimpleMappings(array());
- $this->addFieldMapping('contents', 'data');
- $this->addFieldMapping('filename', 'filename')
- ->dedupe('file_managed', 'filename');
- $this->addFieldMapping('filemime', 'content_type');
- // Since we don't care about the history of the source files, use updated_at instead of created_at.
- $this->addFieldMapping('timestamp', 'updated_at');
- ///////////////////////////////////////////////////////
- // Unmapped Source fields
- ///////////////////////////////////////////////////////
- $this->addUnmigratedSources(array('db_file_id', 'created_by', 'updated_by', 'created_at'));
- ///////////////////////////////////////////////////////
- // Unmapped destination fields
- ///////////////////////////////////////////////////////
- $this->addUnmigratedDestinations(array('uri', 'path', 'uid'));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment